JavaScript · Lesson 05 / 32 · English
JavaScript Comments
Understand the idea. Change the code. Build confidence with one focused experiment.
Read this lesson in தமிழ் ↗.
Start with the idea
Comments explain intent without executing. Use a line comment for a short reason and a block comment when a longer explanation is needed.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
Step-by-step lesson notes
Comments in JavaScript
Comments are lines of code that JavaScript will not execute. They explain the code to other developers — and to your future self.
Single-Line Comments
// This is a single-line comment
console.log("Hello"); // This comment is at end of a line
// Use // to explain WHY, not WHAT
const TAX_RATE = 0.08; // 8% sales tax as per state law
// Temporarily disable code
// console.log("debug output");Multi-Line Comments
/*
This is a multi-line comment.
It spans multiple lines.
Useful for longer explanations.
*/
/*
Temporarily disabling a block of code:
const result = oldSlowFunction();
process(result);
display(result);
*/JSDoc Comments
JSDoc is a documentation standard for JavaScript. IDEs like VS Code use these comments to show inline hints and type information.
/**
* Calculates the total price including tax.
* @param {number} price - The base price in dollars
* @param {number} taxRate - Tax rate as decimal (e.g. 0.08 for 8%)
* @returns {number} The total price with tax
*/
function calculateTotal(price, taxRate) {
return price * (1 + taxRate);
}
// VS Code will now show hints when you call this function:
calculateTotal(100, 0.08); // returns 108Good comments: Explain why something is done, not what the code does. Well-named variables and functions should already tell you what. Comments that repeat what the code says are noise.
A complete example
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
// Delivery is free for this example.
const subtotal = 300;
/* Keep delivery separate so the rule
can change later. */
const delivery = 0;
console.log(subtotal + delivery);Predict first. Reveal the expected result.
300
Make one change. See what happens.
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Wait for the editor to load, then load this example and press Run.
Powered by the free OneCompiler editor. Code you run is sent to that external service. Keep personal data and secret keys out of your examples.
Put the idea to work
Practice challenge
Set delivery to 40 and update the comment to match.
Reveal the answer and reasoning
The result becomes 340. Comments should describe the rule actually used by the code.
One mistake to avoid: A comment cannot repair invalid code outside it. Do not nest block comments.
Your learning checkpoint
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.