Comments are very common to every programming languages, they also come in different formats. Just like in other programming languages, comments are mostly use to add an explanation to a method, or class, it basically makes your code more readable for other developers who will work with you.

Aside from these, comments can also be used to prevent the execution of a certain expression.

Single line comments

Single-line comments starts with //. Any characters between these two slashes up to the end of the single line will be identified as a comment by the JavaScript grammar checker as a comment. In most situations, single line comments are added above the expression that you want to have that comment on.

Example

// adds two argument and return its sum
functions add (a, b) {
  return a + b;
}

You can also add a single-line comment at the end of a expression to save space.

const legalAge = 18; // The minimum age allowed to drink alcohol

Multi-line comments

In some cases, single-line is not enough to cater your explanation about you code. That's where multi-line comments comes into play. Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by JavaScript.

You can put even add an entire paragraph of comment just to explain your complicated code.

Although you have to keep in mind that while it's okay to have a full essay explaining your code, it's always much better if you can write your code beautifully that it doesn't need any explaining. :D That's why single-line comments are more common than multi-line ones. Multi-line comments are used mostly for formal documentations.

Example

/*
This method accepts two arguments, the addends `a` and `b`. it also returns the sum of the given numbers.

Meo miau mi'au mao miauw meong miau myau mjá mňau. Meo mjá mi'au meow. Mi'au miao muwaa' mi'au ngiyaw meo mjá muwaa' miao niaou. Mňau meow meo miyāʾūṉ νιάου ngiyaw myām̥ō mjá. Myau mjau meong. Miyāʾūṉ miyāʾūṉ mňau meo ngiyaw miáú miáú miao. Meo meong miau meo ya-ong miau. Myau miauw ya-ong meo mjá miyav.
*/
functions add (a, b) {
  return a + b;
}
Cat meows generated by https://kitty-ipsum.com

Using Comments to Prevent Execution

You can also use comments to eliminate a line or block of codes that you don't want to be executed. This can be helpful while testing your code. You can just add // in front of your code.

Example (single line)

functions add (a, b) {
  return a + b;
}

// add(2, 2);
add(3, 3);

The commented function invocation will not be executed.

Example (multi-line)

functions add (a, b) {
  return a + b;
}

/*
add(2, 2);
add(3, 3);
*/

Both the first, and second invocation will not be executed.