JavaScript comments are used to explain code and make programs easier to understand. They are ignored by the JavaScript engine and are not executed.
Comments are useful for:
Single-line comments start with //.
Everything after // on the same line is ignored by JavaScript.
The comments are ignored, but the JavaScript statements execute normally.
Multi-line comments begin with /* and end with */.
They can span several lines.
JavaScript ignores everything inside the comment block.
Comments can temporarily disable code while testing.
The commented line will not execute.
| Type | Symbol | Use |
|---|---|---|
| Single-line | // | Short explanations. |
| Multi-line | /* */ | Long descriptions or large code blocks. |
Use comments when the program contains difficult calculations or special logic.
This helps other programmers understand the purpose of the code.
Too many comments can make code difficult to read.
The comments explain the purpose of each section of the program.
| Comment Type | Symbol |
|---|---|
| Single-line | // |
| Multi-line | /* ... */ |
Which symbol is used for a single-line comment in JavaScript?