JavaScript Comments
You can write statements the engine runs and values with careful syntax. This session adds something the engine skips: comments — notes for you and other humans reading the code.
What are JavaScript comments and how do you write them?
A comment is text the JavaScript engine ignores. It does not change what the program does. Use // for a note that ends at the end of that line, or /* … */ for a note that may span one or more lines. Comments explain why, leave reminders, or temporarily switch off a line while you test.
Comment forms at a glance
📊 How beginners should use comments
| Form | Example | When to use it |
|---|---|---|
Single-line // |
// Show a welcome message |
Short note on the same line or the line above |
Multi-line /* */ |
/* several lines of notes */ |
Longer explanation, or several lines at once |
| After code | console.log(x); // temp check |
Tiny reminder next to a statement |
| Disable a line | // console.log(x); |
Temporarily stop a statement without deleting it |
| Not for the engine | (any comment) | Visitors never “run” your comments — only humans read them |
Single-line comments with //
Everything from // to the end of that line is a comment. Code before // on the same line still runs.
console.log("Hello"); // greeting for the Console
console.log(42); // a number literal
You can also put // alone on its own line, above the statement it describes.
// Log a short status message
console.log("Ready");
Multi-line comments with / /
Start with /* and end with */. Everything between them is ignored — even across several lines.
/*
Demo: greet the visitor once.
Keep this short for beginners.
*/
console.log("Welcome");
Temporarily switch off code
Commenting out a line is a common debug habit: leave the code in the file, but stop it from running until you uncomment it.
console.log("This runs");
// console.log("This stays quiet");
console.log("This also runs");
Only the middle line is silenced. Remove the // when you want that log again.
Comments next to page updates
A short why comment helps future-you when a statement updates the page. The comment itself never appears on the page.
<!DOCTYPE html>
<html>
<head>
<title>Comments — Page note</title>
</head>
<body>
<h1 id="msg">Waiting…</h1>
<script>
// Show a clear status once the script runs
document.getElementById("msg").textContent = "Comments help humans";
</script>
</body>
</html>
The heading text comes from the string literal. The // line is only for readers of the source.
Try it yourself
- Add a
//note above aconsole.logand another//at the end of the same line; run and confirm the logs still appear. - Wrap a two-line explanation in
/* */above a log; change the comment text — output should stay the same. - Comment out one of three
console.loglines with//, run, then restore it. - Open the HTML demo; change only the comment wording; refresh and confirm the heading text did not change.
- Write one comment that says why (for example “temporary check”) instead of repeating the code word-for-word.
Common mistakes
- Forgetting the closing
*/after a/*— later code may disappear into the comment - Expecting
//to continue onto the next line (it stops at the line end) - Nesting
/* … /* … */ … */carelessly — multi-line comments do not nest safely - Writing comments that only restate the code (
// log hellonext toconsole.log("hello")) instead of a useful why - Confusing this session with Syntax (literals and names) or expecting Variables already — comments are notes, not storage
Summary
- Comments are for humans; the engine ignores them
//covers the rest of one line/* */can cover one or many lines- Use comments for why, reminders, and temporary disable — not noise
- Next you will learn Variables: names that hold values you can reuse
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top