Javascript: The Definitive Guide

Previous Chapter 2 Next
 

2.4 Comments

JavaScript supports both C-style and C++-style comments. Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript. Also, any text (which may cover multiple lines) between the characters /* and */ is treated as a comment.

In addition, JavaScript recognizes the HTML comment opening sequence <!--. JavaScript treats this as a single-line comment, just as it does the // comment, and does not recognize the HTML comment closing sequence -->. There is a special purpose for recognizing the HTML comment but treating it differently from HTML. In a JavaScript program, if the first line begins <!--, and the last line ends //-->, then the entire program is contained within an HTML comment and will be ignored (instead of formatted and displayed) by browsers that do not support JavaScript. Since the first line begins with <!-- and the last line begins with //, JavaScript ignores both, but does not ignore the lines in between. In this way, it is possible to hide code from web browsers that can't understand it, without hiding it from those that can. Because of the special purpose of the <!-- comment, you should use it only in the first line of your scripts; other uses would be confusing.


Previous Home Next
Optional Semicolons Book Index Literals
 


Banner.Novgorod.Ru