What is a comment in Javascript?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

What is a comment in Javascript?

In programming languages, comments are a way to add a meaningful message in the code that the compiler or the interpreter will not execute. Or simply we can say comments are not useful for computers, but they are useful for readers. Through comments, suggestions, information, or warning about code can be provided by the programmer.

In JavaScript, comments are different by none compared to other programming languages, and the JavaScript engine ignores the comment. Comments are the way by which the code of JavaScript can be written in a way that programmers can add some extra information for tricky steps such that they can remember that or other team members can understand it easily.

Giving suggestions or explanations are only a minor usage of the comments; the major use of comments is for testing or debugging the code. By comments, we can stop the execution of a particular line, part, or block of code by finding the bugs in the code. But, the tester has to use the comments carefully because they may cause some problems also, which we are going to see in the article below.

Types of JavaScript Comments (Description, Syntax, & Example)

We have seen the basics of the javascript comment above, in this section, we are going to see how to add a comment in the code of the JavaScript. There are two ways to add a comment in javascript, or there are two types of javascript comments:

  1. Single-line comment
  2. Multi-line comment

Single-line Comment

As the name suggests, single-line comments can only stop the execution of a single line. To start the single-line comment, we use double forward slashes without spaces (//), and there is no need to indicate the ending of the single-line comment as it is automatically predicted by the editor when the line ends.

Syntax:

In the above syntax, we can see that expression1 is the expression that may present or may not present before the comment starts, and it will be executed, but the expression after the forward slashes will not be executed; that is, expression2. Also, expression3 is present in the next line, so it will be executed.

Let's see an example to see the working of a single-line comment:

Example:

In this example, we will write three expressions and add a single-line comment, and will see how it will work:

Code:

Output:

Explanation:

In the above code, we have implemented the above syntax and logged expression_1 and expression_3 as expression_2 is commented; therefore, it will not be executed as an expression.

Multi-line Comment

Multi-line comments are the next version of the single-line comment and can be used to stop the execution of multiple lines and a block of code. To start the multi-line comment we use a single forward slash (/) followed by an asterisk (*) without spaces (/*), and in a multi-line comment we need to indicate where to end the comment, for that, we use an asterisk (*) followed by a single forward slash (/) without space (*/).

There is no need to indicate the ending of the single-line comment as it is automatically predicted by the editor when the line ends.

Syntax:

In the above syntax, we can see that expression_1 is the expression that may present or may not present before the comment starts and it will be executed, but the expressions between the forward slashes and asterisk combination and asterisk and forward slash combination will not be executed that is from expression_2 to expression_n. Also, expression_(n+1) is present in the same line but after the ending of the comment, so it will be executed, and expression_(n+2) will also be executed.

Let's see an example to see the working of the multi-line comment:

Example:

In this example, we will write some expressions and add a multi-line comment and will see how it will work:

Code:

Output:

Explanation:

In the above code, we have implemented the above syntax and logged expression_1, expression_6, and expression_7 as other expressions are not executed because of the multi-line comment.

Using Comments to Prevent Execution

As we have discussed above that the best use of comments is in testing or debugging the program. In debugging, we usually need to find at which point we are getting a bug so we know a particular section of the block is not related to another section, and it is a bug-free, or we will look into that point later so we can comment that to just focus on the minor part. Commenting on a particular part of code provides the facility not to delete a section that we might need later. Let’s see three different types of commenting techniques to stop the execution of a function.

Commenting Out Function Calls

The function is a block of code that takes some predefined parameters and may return a value after being called by the function call. By commenting out the function call, that function will never be called or go through the execution.

Example:

In this example, we will declare a function and define an expression to call the function and comment on it.

Code:

Explanation:

In the above code, we have created a function add and defined an expression for function call but due to commenting, the calling expression function will not be executed.

A function could be of two types one that returns nothing or simply have void as a return type and another function are which may return something such as a string, object, etc. Let’s see how to comment on the bodies of these functions:

Commenting Out Function Bodies — Without Return Values

If we comment on the whole function (its body as well as its declaration, that is, its name and parameters), then the compiler will ignore the whole code, and if we are not commenting function calling expression, then we will get an error that the function is not declared.

On the other hand, if we comment on the body of the function only, then it will check for the syntax of the function as well as no need and no worries about the function call.

Example:

Let’s write a code and declare a function, then we will comment on its body using multi-line comment.

Code:

Explanation:

In the above code, we have defined a function that will not return any value and commented on its body. We called the function, but nothing will be printed on the console as the function body is commented by multi-line comment.

Commenting Out Function Bodies — With Return Values

Commenting on a function’s body that returns some value is not a good idea because we are commenting on the return statement too, which leads to a return of undefined value, which may cause some extra problems for the tester if not handled well. Also, one possible case to ease the problems is to comment function calls too.

Example:

Let’s write a code and declare a function that is going to return some value; then, we will comment on its body using multi-line comments.

Code:

Output:

Explanation:

In the above code, we have created a function that will return some value and comment its body by using javascript comment ( multi-line comment ). We called the function and printed the return value, which is undefined as there is no return statement present.

Advantages of JavaScript comments

Javascript comments are really beneficial for programmers in many ways. Let's see some of them:

  1. By adding comments, one can make their code more readable and understandable as they provide some extra points on the concept written.
  2. Comments can make teamwork easy as one can understand the tricky code of another teammate if they have added the javascript comments in the code.
  3. For debuggers, comments in javascript are very useful as by commenting on a particular section of code, they can stop the execution of the section and debug the code.
  4. Commenting on the part of code provides the facility of not deleting or writing in another file a section of code for which the programmer is not sure whether to remove that or not.

Writing Effective Javascript Comments

Comments are very beneficial for programmers to add to the code, but they must have to add comments in a better way to get maximum benefit. Let’s see how:

  1. Comments are added to provide an explanation or information about a particular section of code, so it must be given in an ineffective way so that others may understand what is going on in that section.
  2. Sometimes, it's better to add a multi-line comment, and many times, going with the single-line comment is efficient, so both must be used effectively.
  3. Where the single-line comment is possible, try to go for it, but if the comment is too long, then break the comment into multiple lines and add a multi-line comment.
  4. If the comment is short and can be added after the given expression, try to add a single line comment after the expression only now before it.

Conclusion (in points)

  1. In programming languages, comments are a way to add a meaningful message in the code that the compiler or the interpreter will not execute.
  2. Comments are the way by which programmers can add some extra information for tricky steps.
  3. There are two ways to add a comment in javascript, or there are two types of javascript comments:
    1. Single-line comment (// comment)
    2. Multi-line comment (/* comment*/)
  4. To start the single-line comment, we use double forward slashes without spaces (//), and there is no need to indicate the ending of the single-line comment.
  5. To start the multi-line comment, we use a single forward slash (/) followed by an asterisk (\*) without spaces (/\*), and in a multi-line comment, we need to indicate where to end the comment, for that, we use an asterisk (\*) followed by a single forward slash (/) without space (*/).
  6. For debuggers, comments in javascript are very useful, as by commenting on a particular section of code, they can stop the execution of the section and debug the code.