Tutorials Tonight's Logo
Follow Us


Javascript comment is used to write comments within the code to make the code more readable and for future references.

The comments written within the program is ignored and is not executed. So it can be used to prevent some code from execution when you comment it out.

Importance Of Comments In Programming

Importance of comments in programming is as follows:

  1. Comments in code give extra information about the code like some variable, function use, etc.
  2. Commenting code makes code more readable and easy to understand for the developers.
  3. Tricks used within the program can be explained within the comments.
  4. It can stop a block of code from execution if you don't want to execute for some case.
  5. Comments help to understand code when you refer it in future.

Note: Writing comments in code is highly recommended.

How to comment in javascript?

Single Line Comment In Javascript

To create a single line comment in javascript place 2 slashes // in front of the code or the text you which to comment out.

When you place 2 slashes // then the code or text of the current line is ignored until the next line.

// welcome code
console.log("Welcome to TutorialsTonight");

// Show purpose
console.log("Learning Javascript!");
var num = 10; // num represents number
▶ Try It

Using comment to prevent execution

Commenting out code using a single-line comment. Uncomment the code below and run to understand.

for (let i = 0; i <= 10; i++) {
  console.log("Number = " + i);
  //console.log("Square of number = " + i*i + " ");
▶ Try It

Javascript Comment Multiple Lines

When you need to comment out a large block of code then commenting code using single-line comment is quite burdensome. To comment out multiple lines of code use multi-line comment.

multi-line comment starts with /* and ends with */. The example below uses a multi-line comment for better understanding.

/* This is 
a multi-line comment
alert("This is commented");
only code outside this
multi-line comment will run
console.log("Learning multi-line comment");
▶ Try It

Using comment to prevent the execution

Commenting out multiple lines of code using multi-line comments in javascript.

/* alert("this is commented")
console.log("this is also commented")
for(let i = 0;i < 10;i++)
  console.log("all of these are commented");
console.log("This will execute!");
▶ Try It

More Learning

Want to learn more about programming and web development? We have a few recommendations for you.

Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.