JavaScript Statements, Syntax and Comments
Quick answer
Syntax is the set of rules for forming valid source code. A statement is a syntactic unit such as a declaration or expression statement. Comments annotate the source without executing as ordinary statements.
How it works
Use ; at the end of these expression statements. JavaScript can insert semicolons in certain situations, but a newline is not a universal statement separator. A line comment starts with //; a block comment starts with /* and ends with */. Block comments do not nest.
// Show the starting amount.console.log(20);// console.log(99);Expected console output (Node.js):
20The second logging call is commented out.
/* Two quantities are added. Each represents a box. */console.log(3 + 4);Expected console output (Node.js):
7The comment is ignored as ordinary executable code; the addition runs.
Common mistakes
A comment delimiter inside quoted text is just text. An unclosed quotation mark or block comment can cause parsing to fail. Do not store secrets in comments because source code can be inspected.
Practice
Comment out one of two logging calls and predict which output remains.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Statements, Syntax and Comments — Knowledge Check
Review the concept and apply it to a new example.
Which notation begins a line comment?
Which text closes a block comment?
What does console.log("// note") display?
A logging call is entirely inside a line comment. What happens?
Why avoid storing a password in a source comment?