Comments in JavaScript

Single-line CommentsMulti-line Comments

In the Unicode Character Set blog, we have learned about what Unicode is. Unicode helps websites work well with different languages, making the online experience more connected and accessible. In coding, knowing and using Unicode is essential for creating websites that speak a global language. In this blog, we will learn about the Comments and how to use it in JavaScript.


What are Comments in JavaScript

In JavaScript, Comments are used to explain the code and make it more readable. For example, if we have some comments, some information, or some explanation of the code that we want to note down for future reference and we want it not to be a part of the code then we can write that information or code in Comments. We can write comments in two ways: Single Line Comment (//) and Multi-line Comment(/* */).


Ways of Adding Comments

As mentioned in the above section, we can write comments in two ways: Single Line Comment and Multiline Comment


Single-line Comments

In JavaScript, we can create single-line comments using two forward slashes //. Anything following // on the same line will be treated as a comment and ignored by the JavaScript interpreter. Single-line comments are typically used for short explanations or notes on a single line of code. Single-line comments can also be used to comment out the code. Let’s take a look at the example:

//Single-line Comment
const value = 10 //Assigns the value 10 to the variable
console.log("Value is", value); //Print the value of the variable

In the above example, in the first line, we have added a statement starting with two forward slashes i.e. //Assigns the value 10 to the variable is a comment and will be ignored by the JavaScript Interpreter. Same is the case with the //Print the value of the variable statement. Single-line comments can help us describe our code and make it more understandable to ourself and other developers who may be working on the codebase. Let’s take an example

//Single-line Comment
const string = "Welcome" //Assigns the value Welcome to the string variable
//console.log("Value is", value); //Comment out code
console.log("String is", string); // Prints the value of String


Multi-line Comments

In JavaScript, you can create multi-line comments using the /* and*/ characters. Anything enclosed between /* and */ will be treated as a comment and ignored by the JavaScript interpreter. Multi-line comments are suitable for longer explanations or for commenting out multiple lines of code. Let’s take a look at the example

/*
This is a multi-line comment.
Assigning the value 10 to the variable value1
Assigning the value 20 to the variable value2
Assigning the value 30 to the variable value2
Printing the values of the variables on the console
*/
let value1 = 10;
let value2 = 20;
let value3 = 30;
console.log("The value1 is",value1);
console.log("The value2 is",value2);
console.log("The value3 is",value3);

In the above example, multiple lines are added at the start of the code which will be considered as a multi-line comment and will be ignored by the JavaScript Interpreter.

Comments in JavaScript are like helpful notes for developers, explaining or annotating code without affecting how the program runs. They enhance human understanding and serve as documentation.