Comments

Programs are read not only by computers, but also by people: by your colleagues, and by you yourself a few weeks later, when you no longer remember why you wrote something the way you did. That's why programming languages have comments: notes in the code that the computer ignores.

JavaScript has two kinds of comments. A single-line comment starts with two slashes //. Everything after them, up to the end of the line, is ignored. It can be on a line of its own, or at the end of a line with code:

// Prints the price of two tickets
console.log(2 * 150); // 150 is the price of one ticket

A multi-line comment starts with /* and ends with */. Everything between them is ignored, even over several lines:

/*
Author: Tim Novak
Date: March 8, 2024
*/
console.log(2 * 150);

Comments are also useful for switching code off for a while without deleting it. Programmers call this commenting out: put // at the start of a line, and the line no longer runs. When you want it back, just delete the //.

console.log("This line runs.");
// console.log("This one doesn't.");

This prints:

This line runs.

Instructions

At the very top of the program, add a single-line comment that says what the program does, for example // My shopping list.

You already have chocolate at home. Comment out the line that prints chocolate, so that the program prints only:

Shopping list:
bread
milk

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

Back

3/15

Next