While working with Javascript, one of the most important features that you may need to use often is JavaScript Comment. Generally, JavaScript comments are a part of the JavaScript code. You often add multiple JavaScript comments in a JS program, and thus as a beginner, it’s important to understand their significance.
Table of Contents
ToggleAMPA JavaScript comment is a piece of text within the code that conveys some extra information about the code. Also, the comments are not executed like other JavaScript code and thus have little to no impact on the overall execution of the JS code.
JavaScript comments are of two types, that are as follows:
As its name implies, a single-line comment is a comment restricted to only a single line within the code. You can place a single-line comment either before or after a statement within the code.
Syntax:
// You can write a single-line comment here.
Example:
var a = 5; //Declaring variable a with initial value 5var a = 5; var b = 5; // Adding variable a and b, and storing the value in another variable c var c = a + b; // Output the value of c in console console.log (c);
Multi-line comments are different from single-line comments in the way that it allows you to add several comment lines in the code.
Syntax:
/*You can add
multiple comments
here.
*/
Example:
/*This is a script for adding two variables - x and y. We will store the result in another variable z. Also, the result is output in the console. */ var x = 5; var y = 5; var z = x + y; console.log (z);
Comments are an important aspect of every programming language as they allow developers to share information about the code or a specific statement. JavaScript comments allow you to add extra information to the JavaScript code. One particular thing to note about JavaScript comments is that they do not get executed and do not hamper the functionality of the code if added using the proper syntax.
Hi! I am Pankaj, a full-time content specialist and a part-time programmer and marketer. I love to explore new places and also new ideas. I am an inquisitive person with a passion to learn new skills through every possible opportunity that comes in the way.