JavaScript Comments

The comments in JavaScript are added to document the code and improve code readability. Comments also help other programmers to read and understand your code.

You can also comment the code, preventing execution of the code written in comments.

JavaScript ignores the lines of code and text written in comments. The syntax of comments in JavaScript is similar to comments in C programming language.

There are two ways of writing comments in JavaScript.

Single Line Comment

Any text between a // and the end of a line will be ignored and not executed by JavaScript.

This is an example of single line comment before line of code to explain the code.

// Declare myVar, and assign value of 10
let myVar = 10;

This is another example of single line comment at the end of line of code to explain the code. In this case JavaScript will ignore the text after the //.

Let myVar = 10;  // Declare myVar, and assign value of 10
myVar + 1;  // Add value of 1 to myVar

You can also use // to prevent execution of line of code.

//Let myVar = 10; 

Multi-line Comments

Multi-line comments in the code are delimited by /* and */ characters. Such comments can span over multiple lines.  The multi-line comments may not be nested.

This example creates a multi-line comments to explain the following code.

/* This line calls the
   function foo()
*/
foo();

You can also use multi-line comment to prevent execution of block of code.

/*
  let myVar = 10;
  myVar + 1;
*/