How to Write Single and Multi Line Comments in JavaScript
Learn the two ways to write comments in JavaScript: single-line comments and block comments. See clear examples and know when to use each style.
JavaScript comments are notes in your source code that JavaScript ignores when it runs. Use them to explain intent, warn about a tricky decision, or temporarily skip code while debugging.
JavaScript has two comment styles: single-line comments for short notes and multi-line comments for longer blocks.
Single-Line Comments
A single-line comment starts with two forward slashes. Use it when the note belongs to one line of code or one short idea.
// This line is a comment
console.log("Hello!"); // This note is ignoredThe console prints Hello!. The first line is skipped, and the note after the log statement is ignored.
Single-line comments are useful for quick notes, short warnings, or temporarily disabling one line:
let maxAttempts = 3; // Limit failed login attempts
// console.log("Current user:", user);The first comment explains why the number exists. The second line is a disabled debug log, which is useful while testing but should not stay in finished code.
Multi-Line Comments
A multi-line comment starts with a slash-star marker and ends with a star-slash marker. Everything between those markers is ignored, no matter how many lines it spans.
/*
This whole block is a comment.
JavaScript skips each line inside it.
*/
console.log("Code after the comment still runs.");The console prints Code after the comment still runs. The comment block itself produces no output.
Use multi-line comments for longer explanations, file notes, or temporarily disabling a small block while debugging:
/*
Calculate the final order total after discounts.
The returned value should already include tax.
*/
function calculateOrderTotal() {
return 42.5;
}The comment gives context before the function. The function is still complete and runnable, so the example does not rely on placeholder code.
Disabling Code with Comments
During debugging, comments can help you skip a line or block without deleting it:
console.log("Step 1: Starting");
/*
console.log("Step 2: Temporarily disabled");
console.log("Step 3: Also disabled");
*/
console.log("Step 4: Finished");The console prints Step 1 and Step 4 only. Steps 2 and 3 are skipped because they sit inside the multi-line comment.
This is useful when you want to isolate a bug. Remove disabled code before committing if it no longer teaches or protects anything.
Common Mistakes
Nested Multi-Line Comments
You cannot put a multi-line comment inside another multi-line comment. The first closing marker ends the whole comment, and anything after it becomes regular source text again.
/*
Outer comment starts here
/*
This inner block does not work
*/
This line is no longer safely commented
*/This can cause confusing syntax errors. If a block already contains multi-line comments, use single-line comments on each line or remove the inner block first.
Comment Markers Inside Strings
Comment markers inside a string are just text:
const url = "https://example.com";
const pattern = "// not a real comment";
console.log(pattern);The output is the string text itself: // not a real comment. JavaScript only treats comment markers as comments when they appear outside strings.
Outdated Comments
The most harmful comment is one that lies. If you change code but forget to update the comment, the comment becomes misleading.
// Add 10% tax
const taxRate = 0.08;The comment says 10 percent, but the value is 8 percent. Update the comment or remove it.
Single-Line vs Multi-Line
| Style | Syntax | Best For |
|---|---|---|
| Single-line | // comment | Quick notes, inline explanations, disabling one line |
| Multi-line | /* comment */ | Longer notes, file descriptions, disabling blocks |
For most everyday commenting, single-line comments are enough. Reach for multi-line comments when the note needs more than one line.
Once you are comfortable with comments, see how to run JavaScript in the browser and Node.js to try these examples yourself. A JavaScript code style guide can also help you keep comments consistent across a project.
Rune AI
Key Insights
- Single-line comments start with two slashes and end at the line break.
- Multi-line comments start with a slash-star marker and end with a star-slash marker.
- Comments are ignored by JavaScript and do not run as statements.
- Use comments to explain why, not what the code already says.
- Multi-line comments cannot be nested inside other multi-line comments.
Frequently Asked Questions
Do comments affect how fast my JavaScript runs?
Can I nest multi-line comments inside other multi-line comments?
Should I delete comments before putting code into production?
Conclusion
Comments are one of the simplest tools in JavaScript, but using them well makes your code easier to read, debug, and share. Single-line comments handle quick notes and disabled code, while multi-line comments work for longer explanations and temporary block removal. Pick the right style for the job and keep comments clear and current.
More in this topic
Is JavaScript Frontend or Backend? Full Guide
JavaScript is both a frontend and backend language. Learn the difference between browser JavaScript and Node.js, what each is used for, and which one you should learn first.
Learn JavaScript Step by Step Tutorial with Real Examples
Follow a hands-on tutorial that teaches JavaScript by building a real interactive page. Write your first variables, functions, and event listeners with examples you can run.
JavaScript Tutorial: Complete Beginner's Guide to Programming in 2025
A structured learning path for absolute beginners. Learn what to study first, how to practice, and which JavaScript concepts matter most when you are starting from zero.