JavaScript Notes
Master JavaScript comments: single-line //, multi-line /* */, and JSDoc /** */. Learn when and why to comment your code with real examples, best practices & tips.
Comments are the notes you write for yourself and other developers — JavaScript completely ignores them when running your code. Learning to write good comments is one of the first professional habits a beginner should build.
Real World Analogy
Imagine you are reading a textbook. The main text is the code. The sticky notes a student wrote in the margins — "remember this for the exam!" or "this formula is tricky" — are the comments. They add context without changing the content of the book.
How JavaScript Processes Comments Internally
The JavaScript engine removes every comment during the tokenization phase — the very first step of parsing. This means comments have zero performance cost at runtime.
Types of JavaScript Comments
1. Single-Line Comment (//)
Use // to comment out everything to the right on that same line.
// This is a single-line comment
console.log("Hello, World!"); // You can also add it at the end of a line
// console.log("This line is disabled and won't run");
console.log("This line WILL run");Hello, World! This line WILL run
2. Multi-Line (Block) Comment (/* ... */)
Use /* */ when your comment spans more than one line, or when you want to explain a complex block of logic in detail.
/*
This function calculates the total price
after applying a discount percentage.
Author: WoHoTech
Last updated: June 2026
*/
function getDiscountedPrice(price, discountPercent) {
return price - (price * discountPercent) / 100;
}
console.log(getDiscountedPrice(1000, 10));900
3. JSDoc Comment (/** ... */)
JSDoc is a special style of multi-line comment that tools (like VS Code IntelliSense) can read to give you autocomplete tooltips and type hints.
/**
* Calculates the area of a rectangle.
* @param {number} width - The width in centimeters
* @param {number} height - The height in centimeters
* @returns {number} The area in square centimeters
*/
function getArea(width, height) {
return width * height;
}
console.log(getArea(5, 10));50
When you hover over getArea() in VS Code after writing a JSDoc comment, you'll see the parameter descriptions as a tooltip — that's the power of JSDoc.
Comment Types at a Glance
| Type | Syntax | Use Case |
|---|---|---|
| Single-line | // text | Quick notes, disabling one line |
| Multi-line | /* text */ | Long explanations, disabling blocks |
| JSDoc | /** text */ | Function/API documentation |
Internal Diagram – Comment Placement
Commenting Out Code During Debugging
One of the most practical uses of comments is disabling a line temporarily while you debug:
function calculateScore(marks) {
// console.log("Debug: marks received =", marks); // ← disabled after debugging
const grade = marks >= 60 ? "Pass" : "Fail";
return grade;
}
console.log(calculateScore(75));
console.log(calculateScore(45));Pass Fail
Common Mistakes
❌ Mistake 1: Nested Block Comments
You cannot nest /* */ comments — the inner */ closes the outer one too early:
// ❌ WRONG — will cause a SyntaxError
/*
console.log("Hello");
/* This is a nested comment */ ← This closes the outer comment!
console.log("World"); ← This becomes live code unexpectedly
*/✅ Fix: Use single-line comments (//) inside a block comment:
// ✅ CORRECT
/*
console.log("Hello");
// This is a comment inside a block comment
console.log("World");
*/❌ Mistake 2: Commenting Obvious Things (Over-commenting)
// ❌ BAD — explains the obvious
let x = 5; // assigns 5 to x
let y = 10; // assigns 10 to y
let sum = x + y; // adds x and y✅ Fix: Comment the why, not the what:
// ✅ GOOD — explains the business reason
const MAX_LOGIN_ATTEMPTS = 5; // Security policy: lock account after 5 failures❌ Mistake 3: Leaving Outdated Comments
// ❌ BAD — comment says one thing, code does another
// Multiply price by 2
const finalPrice = price * 1.18; // code was updated but comment wasn't!✅ Fix: Always update comments when you change the code.
Best Practices for Writing Comments
- Explain the why, not the what — the code already shows *what*; comments should explain *why*
- Keep comments short and precise — one sentence is usually enough
- Use JSDoc for all public functions — your future self will thank you
- Delete dead commented-out code — use Git to track old code instead
- Write comments in the same language as your team — consistency matters
Key Takeaways
//creates a single-line comment — everything after it on that line is ignored/* */creates a multi-line (block) comment — spans as many lines as you need/** */is the JSDoc style — used for function/API documentation with type info- Comments have zero runtime cost — they're removed during tokenization
- Cannot nest
/* */inside/* */ - Comment the why and intent, not the obvious mechanics
- Use comments to temporarily disable code while debugging — but clean up afterwards
Interview Questions & Answers
Q1. What are the types of comments in JavaScript?
Answer: JavaScript has three types:
- Single-line (
//) — comments out everything to the right on the same line - Multi-line / Block (
/* */) — comments out everything between the delimiters - JSDoc (
/** */) — special block comment used for generating documentation and IDE tooltips
Q2. Are comments executed by JavaScript?
Answer: No. Comments are completely ignored by the JavaScript engine. They are removed during the tokenization phase (the very first step of parsing) and never reach the execution stage. They have zero performance impact.
Q3. Can you nest multi-line comments in JavaScript?
Answer: No. You cannot nest /* */ comments. The first */ encountered closes the comment, even if it was meant to be inside another /* */. This will cause a SyntaxError. The workaround is to use // inside a block comment.
Q4. What is JSDoc and why is it useful?
Answer: JSDoc is a documentation standard using /** */ comments. It lets you describe a function's parameters, return type, and purpose in a structured format. Tools like VS Code IntelliSense read JSDoc and show helpful tooltips while you type, making code easier to understand and use.
Q5. What is the keyboard shortcut to comment/uncomment code?
Answer: In most editors (VS Code, WebStorm): Ctrl + / (Windows/Linux) or Cmd + / (Mac) toggles single-line comments on selected lines. This is one of the most-used shortcuts in daily development.
Q6. What's the difference between a comment and dead code?
Answer: A comment is intentional documentation. Dead code is commented-out source code that's no longer used. Best practice is to delete dead code and rely on Git history to recover it — dead code creates confusion and clutter.
Q7. How do comments affect JavaScript file size?
Answer: Comments increase the source file size but are completely stripped during minification (tools like Terser/UglifyJS). In production, minified files have no comments, so there's no impact on the JavaScript delivered to users.
Q8. Why should you comment the "why" not the "what"?
Answer: The what is already visible in the code itself (e.g., x = x + 1 clearly adds 1). The why is the business logic or intent that isn't obvious from reading the code (e.g., "increment retry count — max retries is 3 per security policy"). Good comments add information the code alone cannot convey.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Comments Complete Guide – Single Line, Multi-line & JSDoc with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, basics, comments, javascript comments complete guide – single line, multi-line & jsdoc with examples
Related JavaScript Master Course Topics