Comments are lines in your code that JavaScript completely ignores when running your program — they exist purely for the humans reading the code, whether that's a teammate, your future self, or you tomorrow morning when you've forgotten exactly why you wrote something a certain way.
Single-Line Comments
A double slash // turns the rest of that line into a comment:
// Calculate the total price including tax
let price = 100;
let tax = 0.08;
let total = price + price * tax; // 8% sales tax
console.log(total);
Output:
108
Multi-Line Comments
For longer notes, or to temporarily disable a block of code, use /* to start and */ to end — everything in between is ignored, even across multiple lines:
/*
* This function calculates a discounted price.
* discountPercent should be a number between 0 and 100.
*/
function applyDiscount(price, discountPercent) {
return price - price * (discountPercent / 100);
}
console.log(applyDiscount(200, 25));
/*
console.log("This whole line is disabled");
console.log("So is this one");
*/
Output:
150
This "commenting out" trick is extremely common while debugging — temporarily disabling a line or block without deleting it, so you can test whether removing that logic fixes (or causes) a problem.
When to Comment — and When Not To
Good comments explain why something is done a certain way, not what the code does — if the "what" isn't obvious from reading the code itself, that's often a sign the code could be written more clearly instead.
// Bad: states the obvious, adds no value
let x = 5; // set x to 5
// Good: explains a non-obvious reason
let retryLimit = 3; // API occasionally fails once or twice under load, so we retry
As you write more real code — starting with JavaScript functions — you'll naturally develop a sense for which lines genuinely need an explanation and which are already clear on their own.
