JavaScript Comments

A comment is a note in your code that JavaScript ignores. Use comments to explain why the code does something.


Single-line comment

Start with two slashes //. Everything after them on the same line is ignored.

// This is a comment
let price = 100;   // a comment after the code
console.log(price);
Output of the above program is

100


Multi-line comment

Start with /* and end with */.

/*
  This is a multi-line comment.
  It can go on many lines.
*/
console.log("Hello");
Output of the above program is

Hello


Comment out code

Add // in front of a line to switch it off for a while.

console.log("Visible");
// console.log("Hidden");
Output of the above program is

Visible

Note: Write comments to explain the why, not the what. A comment like // add 1 to i next to i++ does not help. In editors the shortcut is Ctrl + / (Cmd + / on Mac).

Summary

  • // for one line, /* */ for many lines.
  • Comments are ignored by JavaScript.
  • Next: JavaScript Console.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Write a JavaScript program to display Hello World
Interview Questions & Answers
What is JavaScript?
What is strict mode in JavaScript?