JavaScript Comment

javascript-comment

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.

What is a JavaScript Comment?

A 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.

Types of JavaScript Comments

JavaScript comments are of two types, that are as follows:

1. Single-line Comment

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:

  • Single-line comment added after a statement:
var a = 5; //Declaring variable a with initial value 5
  • Single-line comment added before a statement:
var 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);

2. Multi-line Comment

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);

Takeaway

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.