Comments in Javascript

Published on: July 03, 2021 By T.Andrew Rayan

Comments in javascript is used by the developer to give explanation to the code. It describes what the code or the function doe. The comments does not execute.

There are 2 types of comments

1) Single-line comments

2) Multi-line comments

Single-line Comments:

The single line comments are represented by the notation //.

The // notation followed by the string comments will not be executed. It works only for a single line.

Example for single line comments

We can also use the single line comment to the end of the code to explain its purpose.

var discountPercentage; // variable used to get discount percentage.

Multi-line Comments:

Multi-line comments have one or more lines within a set of comment delimiters. The delimiters starts with /* and ends with */ delimiter notation.

Like single-line comments, multi-line comments also will not execute by the program.

Example for mulit-line comments

In the above code we have a function calculateDiscount and the comments explains about the operation performed by it and the description about the parameters.

So now when the other developer works on the code, he would be able to understand its purpose. Though the function is small and clear explains its operation not all the functions will be simple as this. So in such cases the comments will be helpful and saves most of the time in understanding the code.

Most Read