JavaScript ยท Chapter 6 of 55
JavaScript Comments
Comments let you annotate code for humans; the JavaScript engine ignores them completely. Good comments explain WHY code exists, not just WHAT it does.
JavaScript supports single-line comments with `//` and multi-line comments with `/* ... */`.
Single-line comments
Everything after `//` on a line is ignored. Use them for brief notes next to code.
Multi-line comments
Wrap longer explanations in `/* */`. These are also used to temporarily disable blocks of code during debugging.
Example 1 (javascript)
// Calculate total price
let total = 100 * 1.2; // apply 20% tax
console.log(total);Output
120Both comment styles appear here.
Example 2 (javascript)
/*
This function greets a user
by name.
*/
function greet(name) {
return "Hi " + name;
}A multi-line comment documents the function above it.
Key points
- `//` starts a single-line comment.
- `/* ... */` wraps multi-line comments.
- Comments are ignored entirely at runtime.
- Use comments to explain intent, not obvious code.
๐ก Note: Avoid leaving large blocks of commented-out code โ use version control history instead.
