JS Comments
In this page:
Single Line Comments
A single line comment starts with two forward slashes //, and everything from that point to the end of the line is ignored by the JavaScript engine.
Note: Use single line comments for short, quick notes right above or beside the line of code they explain.
Warning: A // inside a string, like a URL, is just text and does not start a comment, comments only apply to actual code.
Example: Single Line Comments
// This line explains the code below
let age = 25;
console.log(age);
Multi Line Comments
A multi line comment starts with /* and ends with */, letting a comment span several lines, which is useful for longer explanations or temporarily disabling a block of code.
Note: Use multi line comments for longer explanations, like describing the purpose of an entire function or file.
Warning: Multi line comments cannot be nested, starting a new /* inside an existing comment does not create an inner comment and can cause confusing errors.
Example: Multi Line Comments
/*
This is a longer explanation
spanning multiple lines.
*/
console.log("Done");
Commenting Out Code
Comments are commonly used to temporarily disable a line or block of code without deleting it, which is useful while debugging or testing alternate approaches.
Note: Most code editors let you comment out a selected block with a keyboard shortcut, making this technique fast to apply while testing.
Warning: Remember to remove or restore commented-out code once you're done testing, leaving it in permanently makes the file confusing for others.
Example: Commenting Out Code
console.log("This runs");
// console.log("This is disabled for testing");
Writing Useful Comments
The most valuable comments explain why a piece of code exists or why a particular approach was chosen, since the code itself already shows what it does, comments should add context that isn't obvious from reading it alone.
Note: Before writing a comment, ask whether it explains something the code doesn't already make clear, if not, it may be unnecessary.
Warning: Comments that simply restate the code, like '// set x to 5' above 'const x = 5;', add clutter without adding real value.
Example: Writing Useful Comments
// Using 0.9 because the API rounds up otherwise
const threshold = 0.9;
console.log(threshold);
JSDoc Basics
JSDoc is a structured comment style, starting with /**, that documents a function's purpose, parameters, and return value in a standardized format many editors and tools can read to provide helpful hints.
Note: Even a simple JSDoc comment above a function can make autocomplete suggestions in your editor far more helpful.
Warning: JSDoc comments must start with exactly /** (two asterisks) to be recognized by tools, a single /* is treated as a regular comment.
Example: JSDoc Basics
/**
* Adds two numbers together.
* @param {number} a
* @param {number} b
* @returns {number}
*/
function add(a, b) {
return a + b;
}
console.log(add(2, 3));
- Writing comments that just repeat what the code obviously does, instead of explaining why it does it.
- Forgetting to close a multi-line comment with */, which accidentally comments out all the code that follows.
- Leaving large blocks of old, commented-out code in a file indefinitely, cluttering it instead of deleting unused code.
- Single-line comments start with //, and everything after them on that line is ignored.
- Multi-line comments are wrapped in /* and */, and can span as many lines as needed.
- Comments are useful for explaining tricky logic and temporarily disabling code while testing.
Both // and /* */ comment syntax are part of core JavaScript and are supported identically by every browser and JavaScript engine.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: