Page 185 - JavaScript
P. 185
Chapter 20: Comments
Syntax
• // Single line comment (continues until line break)
• /* Multi line comment */
• <!-- Single line comment starting with the opening HTML comment segment "<!--" (continues
until line break)
• --> Single line comment starting with the closing HTML comment segment "-->" (continues
until line break)
Examples
Using Comments
To add annotations, hints, or exclude some code from being executed JavaScript provides two
ways of commenting code lines
Single line Comment //
Everything after the // until the end of the line is excluded from execution.
function elementAt( event ) {
// Gets the element from Event coordinates
return document.elementFromPoint(event.clientX, event.clientY);
}
// TODO: write more cool stuff!
Multi-line Comment /**/
Everything between the opening /* and the closing */ is excluded from execution, even if the
opening and closing are on different lines.
/*
Gets the element from Event coordinates.
Use like:
var clickedEl = someEl.addEventListener("click", elementAt, false);
*/
function elementAt( event ) {
return document.elementFromPoint(event.clientX, event.clientY);
}
/* TODO: write more useful comments! */
Using HTML comments in JavaScript (Bad practice)
HTML comments (optionally preceded by whitespace) will cause code (on the same line) to be
https://riptutorial.com/ 142

