JavaScript Comments

JavaScript Comments are a series of text which are not executed by the interpreter or compiler as a part of the code. They are basically used by the programmer to make their code more readable and to deliver some information regarding their code.

In other words, comments are human readable explanation about the code that the developer is writing. Programming is a collaborative task which is why more than one programmers might be working on the same piece of problem. So, it is only normal to make sure that the code you are writing can be understood by other programmers 🙏.

Therefore, we write comments so that other developers can understand what the code does and your intention behind it before even reading the code.

Sometimes we write comments just for ourselves because we might forget why we wrote that code and our intention behind it after a couple of days or weeks 😅. There are multiple ways to write different types of JavaScript comments. Here are some of the ways.


Types of JavaScript Comments

There are two types of comments or commenting in JavaScript. They are as follows:

Note: Make sure to check out our 👉 JavaScript Console lesson.


Single-Line Comment 1️⃣

A single-line comment in JavaScript is used to give only one line of comment. We can produce a single-line comment by using two forward slashes. i.e. //.

Syntax

//This is a single-line comment
console.log("Hello World");

The text //This is a single-line comment will not be executed as part of the code by the interpreter or compiler. It will be ignored by the interpreter when our code runs. It is just written for the developer so that they can understand the intention behind the code.

Example: Writing comments to show our intention behind that code

//Displays "Hello World" to the console
console.log("Hello World");

Output

Hello, World

Example: We can also write single-line comments after a line of code

console.log(99);  //Displays the number 99 to the console
99

Multi-Line Comment ✌

A multi-line comment in JavaScript is used to give more than one line of comment. It is basically written to give a more detailed explanation to the code. We can start a multi-line comment by using /* and end it using */.

Syntax

/*
This is a multi-line comment.
We can enter multple lines of comments.
*/
console.log("Hello World");

All the text inside /* */ will will be ignored by the interpreter when our code runs.

Example: Writing a detailed explanation about our code

/*
Displaying my age.
Also, displaying my height.
*/
console.log("My Age is");
console.log(25);
console.log("My Height is");
console.log("5 ft. 9 inch.");

Output

My Age is
25
My Height is
5 ft. 9 inch.

Example: We can also comment out a whole piece of code so that the code will not be executed and will be ignored

/*
Commented code do not get executed.
console.log("Hello, Friends");
console.log("My Age is");
console.log(25);
So, none of this will run.
*/
console.log("My Name is");
console.log("Bitslord");

Output

My Name is
Bitslord

Commenting a piece of code is mostly done for debugging process. We can make sure that the code which might be causing errors do not run by simple putting it inside a multi-line comment.


In this way, commenting helps us write more readable code and makes collaboration among programmers easier and efficient.