Chapter I · The Language
Comments
Comments are human-readable notes ignored by the JavaScript engine, used to explain code intent and document logic.
01 What are Comments?
Comments are explanatory notes written directly inside source code. The JavaScript engine completely ignores them during parsing and execution.
They can be written as single-line comments using // or multi-line block comments using /* ... */.
02 Simple Example
Documenting code logic with descriptive comments.
// Initialize user score counter
let score = 100;
/*
Calculate final multiplier bonus
applied at stage end
*/
let bonus = score * 2;
The engine executes the variable assignments cleanly while completely bypassing the text notes.
03 How It Works
During the parsing phase, lexical analyzers strip out comment tokens so they never reach the Abstract Syntax Tree or execution bytecode.
Comments are filtered out during parsing, leaving only executable instructions.
04 Step-by-Step
The developer writes comments to document intent or temporarily disable code.
The engine lexer identifies comment markers and discards the text content.
Execution proceeds normally with zero performance or runtime footprint.
05 Mental Model
"Margin notes left in a textbook for human readers while the machine reads strictly core text."
They help developers communicate context to one another without interfering with code behavior.
06 Common Mistakes
Nesting block comments. /* ... /* ... */ ... */ doesn't work the way you'd expect — the first */ closes the whole comment, and anything after it is treated as live code, often causing a syntax error.
Best practice: write comments that explain why code exists, not what it does — the code itself already shows what it does. "Retry 3 times because the payment API is occasionally flaky under load" is far more useful than "// loop 3 times".
07 Interview Questions
Q: Do comments affect performance?
No — they're stripped during parsing and have zero runtime cost, though very large comment blocks can marginally affect download size before minification removes them.
Q: What's the difference between // and /* */?
// comments out to the end of the line; /* */ can span multiple lines and is also used for JSDoc-style documentation comments.
Key Takeaway
Comments provide vital human documentation while being completely ignored by the JavaScript engine during execution.