JavaScript Comments | Single line, Multiline

Comments in JavaScript are statements we write as part of our code to communicate something to humans.

They are human-readable descriptions or explanations of code that we add to our script code to make the script easier to understand and maintain.

JavaScript comments allow the programmers to constitute and express their thoughts related to the script code independently.

It is a good habit to write comments related to script code in JavaScript program. It can be also useful when someone tries to understand the script code.

For example, here is a comment that expresses the value used to initialize a variable “x”:

let x = 50; // Variable declaration and initialization.

This comment explains the declaration and initialization of a variable with a value. Comments in JavaScript can also be useful to hide program code.

Like other languages, JavaScript comments are completely ignored by JavaScript interpreter embedded in the browser. That is, comments do not get executed like all other code we write. JavaScript completely ignores comments.

Types of Comments in JavaScript

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

  • Single line comment
  • Multiline comment

Let’s understand one by one with examples.

Single Line Comments

Single line comment is represented by double forward slashes (//). It can be used before and after a text. That is, placing two slashes (//) before and after the statement makes it a single line comment.

Let’s consider a simple example where we will place a single line comment before a statement.

    // This is a single line comment.
         document.write("JavaScript Programming");

We can also write single line comment as one line as:

let x = 25; // Here, x is a variable that is initialized with a value.

Let’s take a simple JavaScript program, in which we will write a single line comment before and after statements.

Program code 1:

<DOCTYPE html>
    <title>Single Line Comment Example</title>
    // Variables declaration and initialization.
      var x = 20;
      var y = 30;
      var z = x + y ; // It will add the values of variables.
      document.write("Sum: " +z); // This statement will print the sum of two numbers.
Output of code:
         Sum: 50

MultiLine Comment

Sometimes a single line comment is not enough for the description of script that we want to provide. In a such situation, we can multiline comment.

Multiline comments begin with /* and end with */. Any statement between /* and */ will be considered as multiline comment and is ignored by JavaScript engine.

This is an example of multiline comment below:

/* This is multiline comment.
   It will not be executed by JavaScript interpreter. */

Let’s take a simple JavaScript program, in which we will write a multiline comment to provide the information of the code.

Program code 2:

<DOCTYPE html>
    <title>Multi-Line Comment Example</title>
      /* JavaScript is a simple web programming to display
        text in a browser. */
      document.write("Hello! I am Deepak and a Web programmer in class 10." +"<br>");
      document.write("I am in RSVM, Dhanbad");
Output of code:
       Hello! I am Deepak and a Web programmer in class 10.
       I am in RSVM, Dhanbad

Using Comments to Prevent Execution of Code

We can also use comments to forbid execution of code is suitable for code testing. Placing a double forward slash (//) in front of a code line changes an executable code line to a comment line.

The below example uses // to prevent execution of one of the code lines:

     document.write("Hello! JavaScript World"); // Executable code line.
// document.write("Hello! JavaScript World"); // Now, it is a comment line.

Commenting Best Practices

Let’s point out some important points about how to properly use comments to make our code easy to read:

1. Always comment on your code as you are writing it. Writing comment may be boring for you, but it is an essential part of the code.

2. Don’t defer comment for writing later when you’re writing code. It is not a good thing.

3. Use more English word and less JavaScript.

4. Do not complicate your comment unnecessarily with the code. Be clear and concise.

5. If a line of code is self-explanatory, do not waste your time to comment about obvious code.

These are some important points that you must keep in mind for best practices.

Advantage of JavaScript Comments

There are many advantages to adding comments that the script code follows. They are:

1. JavaScript comments help to make the script code more readable and understandable.

2. It helps the programmers to make the script maintainable.

3. Comments also help to hide unnecessary code. Sometimes, we add the script code to perform some function. But after sometime, function may not be necessary and need to disable the code. In such case, comments help to hide the code by commenting the script code.

Hope that this tutorial has elaborated enough points related to comments in JavaScript with a lot of examples. I hope that you will have understood the basic points for best practices.
Thanks for reading!!!
Next ⇒ JavaScript keywords⇐ Prev Next ⇒

Please share your love