Multi-line Comments in JavaScript

Question
What are the dos and don’ts of multi-line comments in JavaScript?

Answer
Dos:
-Be sure to use them when the text required to describe code spans several lines.
-Place them immediately before the code they pertain to.
-Make them more legible by aligning asterisks.
-Precede them with an empty line.
-Use the same indentation level for them as the code they explain.

Don’ts:
-Don’t use several single-line comments to achieve a multi-line comment effect.
-Don’t use them for trailing comments.

Examples

// Correct.
if (someCondition) {

  /*
   * If you're here,
   * then you have access.
   */
  grantAccess();
}

// Incorrect. Missing preceding empty line.
if (someCondition) {
  /*
   * If you're here,
   * then you have access.
   */
  grantAccess();
}

// Incorrect. Missing a space after the asterisks.
if (someCondition) {
  /*
   *If you're here,
   *then you have access.
   */
  grantAccess();
}

// Incorrect. The indentation is wrong.
if (someCondition) {
/*
 * If you're here,
 * then you have access.
 */
  grantAccess();
}

// Incorrect. A trailing multi-line comment.
let operandTwo = 0;
let answer = operandOne / operandTwo; /* Division by ZERO */

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s