Comments in JavaScript
Introduction
Comments are notes in source code that the JavaScript engine ignores at runtime. They do not change program output by themselves, but they make code understandable for future you, teammates, and reviewers. Learning comment styles early pairs directly with coding standards.
Prerequisites
- Syntax overview
- Statements
- Ability to run
.jsfiles with Node.js
What Is a Comment
A comment documents intent for humans. The interpreter skips comment text when executing statements.
// This line runs
console.log("Hello");
// The engine skips this explanation entirelySingle-Line Comments (//)
The most common style—everything after // on that line is a comment:
// Maximum login attempts allowed per hour
const maxAttempts = 5;
// Log the configured limit for debugging
console.log(maxAttempts);Multi-Line Comments (/* */)
Use block comments for longer explanations or temporarily disabling code during experiments:
/*
Business rule (v1):
- Free tier: 3 projects
- Pro tier: unlimited projects
*/
const tier = "free";
const projectLimit = tier === "free" ? 3 : Infinity;
console.log(projectLimit);Tip
Best Practice
Comment why a rule exists, not what obvious code already states.
If the code says total = price * quantity, avoid commenting "multiply price and quantity" unless tax or rounding rules need context.
Inline Comments (Use Sparingly)
Inline comments sit on the same line as code:
// Inline example: clarify non-obvious unit
const timeoutMs = 5000; // 5 seconds in millisecondsUse them when they add quick context. Avoid clutter on every line.
Commenting Out Code Temporarily
Block comments can disable code while debugging:
const featureEnabled = true;
if (featureEnabled) {
console.log("Feature on");
}
/*
console.log("Old experimental path");
runLegacyPipeline();
*/Prefer deleting dead code before merging to main branches—Git history keeps old versions.
JSDoc-Style Comments (Preview)
For functions and libraries, teams use structured /** ... */ blocks (JSDoc):
/**
* Return the total price before discount.
* @param {number} unitPrice
* @param {number} quantity
* @returns {number}
*/
function calculateTotal(unitPrice, quantity) {
return unitPrice * quantity;
}
console.log(calculateTotal(12, 3));You will meet JSDoc again with functions and TypeScript-oriented tooling. For now, // and /* */ are enough.
Real Mini Example: Guess Validation
// Secret number for the guessing game
const secret = 42;
// Track how many guesses the player used
let guesses = 0;
// Simulated user input
const attempt = 40;
guesses = guesses + 1;
// Compare attempt with secret and give feedback
if (attempt === secret) {
console.log(`Correct in ${guesses} tries`);
} else if (attempt < secret) {
console.log("Too low");
} else {
console.log("Too high");
}Comments vs Output
- Comments: never shown to end users; ignored when running
console.log: runtime output for developers or logs
Do not confuse the two when debugging.
FAQ
Do comments slow down programs?
No meaningful slowdown in normal apps—the engine strips or ignores them during execution.
Can comments nest in JavaScript?
/* */ blocks cannot nest cleanly. Use multiple // lines instead of nested block comments.
Should I comment every line when learning?
Comment important why decisions, not every syntax token. Over-commenting hides the actual code.
Are HTML comments the same as JavaScript comments?
No. <!-- --> is for HTML. Inside <script> tags you use // and /* */.
What is the next topic?
Identifiers and Reserved Words, then variables and data types in the following section.