Comments in Javascript (with Example)

In this tutorial, you will learn about javascript comments and how to use them with the help of multiple examples.

Comments in javascript are used for different purposes. You can use javascript comments for adding instructions, explaining code, debugging, etc. Comments are totally ignored by the javascript engines. 

In javascript, there are two ways to add comments in your code : 

// – Single Line Comments

/**/ – Multi-line Comments

Single Line Comments in Javascript

In javascript, single-line comments start with //. For example,

let x = 10;
// displaying x on the console

Here, // displaying x on the console is a single-line comment.

We can also add single-line comments like this:

let x = 10
console.log(x) // displaying x on the console

Multi-line Comments in javascript

In Javascript, multi-line comments start with /* and end with */. For example

In this javascript program, we are adding two numbers.
Variable a is holding the 5 and variable b is also holding
the 5. So the sum of both numbers will be 10.

let a = 5, b = 5;
console.log( a + b );

Multi-line comments are very useful when you want to add an explanation about a piece of code in multiple lines.

Why do we use Javascript Comments?

As I said in the beginning javascript comments are used for different purposes. But the main purpose of JavaScript comments is to add explanation or annotation to your code so that anyone who will read your code can understand it clearly.

Comment increases code readability and helps you to maintain your code in the future.

Using Comments for Debugging

Comments are also be used to check your code for errors. You can prevent an error-prone code from executing just by commenting it out. For example,

console.log(“normal code”);
console.log(“error code”);
console.log(“normal code”):

So if you are getting any error in your javascript code then instead of removing it you can also comment it out.

console.log(“normal code”);
// console.log(“error code”);
console.log(“normal code”):


How to comment out in Javascript shortcut?

There is a shortcut for adding comments to your code. The shortcut is similar for most code editors. You can use Ctrl + / in windows and Cmd + / in Mac.

How to comment out multiple lines in Javascript Vscode?

To comment out multiple lines you can wrap them inside /* your code */ . For example, /* your code */.

Which symbol is used for comments in Javascript?

// your code is used for adding single-line comments and /* */ for adding multi-line comments.

Leave a Comment

Your email address will not be published. Required fields are marked *