JavaScript Tutorial · JavaScript Basics · Lesson 4 of 44

JavaScript Comments

Single-line and multi-line comments, and when to write them.

Single-line comments

Anything after // on a line is ignored:

Example 1Try it yourself
// Print a greeting
console.log("Hi!"); // a comment can follow code
// console.log("This does not run");

Output

Hi!

Multi-line comments

Text between /* and */ can span several lines. JSDoc comments (/** ... */) above a function document it — editors show them as tooltips and use them for type hints.

Example 2Try it yourself
/*
  Calculate the area of a rectangle.
  Both sides are in centimetres.
*/
/**
 * @param {number} width
 * @param {number} height
 * @returns {number}
 */
function area(width, height) {
  return width * height;
}
console.log(area(3, 4));

Output

12

Commenting out code

Putting // in front of a line is a quick way to stop it running while you debug — but delete dead code before you commit it.

Test yourself

Which characters start a single-line comment in JavaScript?

  1. #
  2. //
  3. --
  4. <!--
Show answer

Answer: //