Comments are text in your code that JavaScript completely ignores. They exist only for humans. They have two jobs: explaining code so it is easier to read (including by you, three weeks from now), and switching code off temporarily while you test something.
A single-line comment starts with //. Everything from the // to the end of that line is ignored.
You can put a comment on its own line, above the code it describes:
// Set up the prices
let price = 100;
let tax = 0.08;
// Work out the total including tax
let total = price + (price * tax);
Or at the end of a line of code, to explain just that line. This is called an end-of-line or inline comment. The code before the // runs; the rest of the line is skipped:
let x = 5; // Declare x, give it the value of 5
let y = x + 2; // Declare y, give it the value of x + 2
console.log(y);
// is just two characters of text. let site = "https://citicoding.org"; works fine and the whole address is stored. JavaScript only treats // as a comment when it appears in the code itself, outside any string.
For longer explanations, start with /* and end with */. Everything between them is ignored, however many lines it covers. This is also called a block comment:
/*
Converts a temperature from Celsius to Fahrenheit.
Formula: (C × 9/5) + 32
The result is rounded to one decimal place.
*/
function toFahrenheit(celsius) {
let f = celsius * 9 / 5 + 32;
return Math.round(f * 10) / 10; // round to 1 decimal
}
console.log(toFahrenheit(100));
console.log(toFahrenheit(37));
A block comment can also sit in the middle of a line, which a // comment cannot: let area = width /* in meters */ * height;. That is rare, but you will see it.
Single-line comments are by far the most common. Block comments tend to be used for longer, more formal documentation, such as describing what a function does at the top of it. Many projects use a special form that starts with /**. Editors like VS Code read it and show your description as a tooltip wherever the function is used:
/**
* Adds tax to a price.
* @param price the price before tax
* @param rate the tax rate, e.g. 0.08 for 8%
*/
function withTax(price, rate) {
return price + price * rate;
}
To JavaScript, /** ... */ is just an ordinary block comment. The extra star and the @param lines are a convention called JSDoc, read by tools, not by JavaScript itself.
*/ JavaScript finds. So if you wrap code that already contains a block comment inside another one, the outer comment ends early and the leftover text becomes broken code:
/* outer comment
/* inner comment */
this line is no longer inside a comment
*/
Result: a SyntaxError. When commenting out code that contains block comments, use // on each line instead.
Putting // in front of a line of code turns it from something that runs into a comment. This is called commenting out code, and it is one of the most useful testing tricks there is: you can switch a line off without deleting it, and switch it back on by removing the //.
let score = 85;
// score = score + 10; switched off for now
console.log(score);
Remove the // and run it again: the output becomes 95.
To switch off several lines at once, wrap them in a block comment:
let lives = 3;
/*
lives = lives - 1;
lives = lives - 1;
*/
console.log(lives);
This is a real debugging technique. When something is broken and you do not know which line is the cause, comment out half the suspect code. If the problem goes away, it was in the half you switched off. Keep halving until you find the line.
//. Press it again to remove them.
Good code mostly explains itself through clear names. Save comments for the things the code cannot say on its own, usually why something is done:
// Bad comment: just repeats what the code already says
let age = 18; // set age to 18
// Good comment: explains WHY
let minAge = 18; // legal voting age in this state
minAge = 18 does. The comment is useful because it says where 18 comes from.What does this log?
let total = 10;
total = total * 2; // total = total + 100;
console.log(total);
20. The code before the // runs. Everything after it, including total + 100, is ignored.What does this log?
let msg = "Visit //citicoding.org";
console.log(msg);
Visit //citicoding.org. The // is inside the quotes, so it is part of the string, not a comment.What does this log?
let n = 1;
/* n = 5;
n = 10; */
n = n + 1;
console.log(n);
2. Both lines inside /* */ are switched off. Only n = n + 1 runs, so 1 + 1 is 2.//. Which value of x is logged, and why?// from line 2 and run it again. What changed?/* */, and predict the result before you run itCtrl+/ (Cmd+/ on a Mac) to comment them all out. Press it again to switch them back on. The same shortcut works in VS Code