Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How To Comment In JavaScript

Introduction

When you're learning how to program, it's essential to learn how to write clean, easy-to-understand code. One crucial aspect of writing clean code is adding comments to your code. Comments are bits of text that help you and others understand the purpose and functionality of the code written. In this blog post, we'll cover what comments are, why they are important, and how to write comments in JavaScript.

What are comments?

Comments are lines of text in your code that are not executed by the computer. They're meant for human readers, like yourself and other developers, to help understand the code better. Think of them as notes or explanations that you leave behind to make your code more readable and maintainable.

Why are comments important?

Comments play a vital role in the software development process, especially when working with others or maintaining your code in the long run. Here are some reasons why comments are important:

Clarity: Comments make your code easier to understand, which can be especially helpful when you revisit a project after some time or when someone else is working on your code.

Collaboration: When working in a team, comments help your teammates to understand your code quickly, reducing the time spent on deciphering complex code.

Maintenance: As your codebase grows, it becomes increasingly difficult to keep track of all the intricacies. Comments help you remember what each part of the code does, making it easier to maintain and debug.

Learning: When you're learning to program, reading well-documented code can be a valuable learning resource. Comments can help you understand how a piece of code works, or why a certain approach was chosen, allowing you to learn from others' work.

How to write comments in JavaScript

In JavaScript, there are two types of comments: single-line comments and multi-line comments. Let's take a closer look at both types and see how to use them in your code.

Single-line comments

Single-line comments, as the name suggests, are comments that span only one line in your code. To create a single-line comment, simply add two forward slashes (//) at the beginning of the line you want to comment. Anything after the slashes will be considered a comment and will not be executed by the JavaScript engine.

Here's an example of a single-line comment in JavaScript:

// This is a single-line comment

You can also add a single-line comment after a line of code, like this:

let sum = 5 + 10; // This line adds two numbers and stores the result in the variable "sum"

Multi-line comments

Multi-line comments, as the name suggests, can span multiple lines in your code. To create a multi-line comment, start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). Everything between these delimiters will be considered a comment and will not be executed by the JavaScript engine.

Here's an example of a multi-line comment in JavaScript:

/* This is a
   multi-line
   comment */

Best practices for writing comments

Now that you know how to write comments in JavaScript, let's talk about some best practices to follow when writing comments:

Be concise and clear: Keep your comments short and to the point. Use clear language to explain the purpose and functionality of the code.

Avoid obvious comments: Don't write comments that just restate what the code does. Instead, focus on explaining the why and how of the code.

Update comments when updating code: Make sure your comments stay relevant as your code evolves. If you make a change to your code, update any related comments to reflect the new behavior.

Use consistent formatting: Adopt a consistent style for your comments, such as capitalizing the first letter and using proper punctuation. This will make your comments look clean and professional.

Comment as you write code: It's easier to write comments while you're writing the code itself, rather than going back later to add them. This helps ensure that your comments are accurate and up-to-date.

Examples of good comments

Here are a few examples of useful comments in JavaScript:

Explaining a complex function

/**
 * Calculates the factorial of a given number.
 * A factorial is the product of all positive integers less than or equal to the number.
 * For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
 *
 * @param {number} num - The number to calculate the factorial of
 * @returns {number} The factorial of the given number
 */
function factorial(num) {
  if (num === 0) {
    return 1;
  } else {
    return num * factorial(num - 1);
  }
}

Explaining a tricky piece of code

function mergeSort(arr) {
  if (arr.length <= 1) return arr;

  // Find the middle index to divide the array into two halves
  const middle = Math.floor(arr.length / 2);

  // Recursively split and sort the left and right halves of the array
  const left = mergeSort(arr.slice(0, middle));
  const right = mergeSort(arr.slice(middle));

  // Merge the two sorted halves into a single sorted array
  return merge(left, right);
}

Explaining the purpose of a variable

const TAX_RATE = 0.07; // Sales tax rate as a decimal (7%)

Conclusion

Writing comments is an essential skill for any programmer, especially when working with others or maintaining code over time. By following the best practices and examples provided in this post, you'll be well on your way to writing clean, well-documented JavaScript code. Remember, the key is to be concise, clear, and consistent with your comments. Happy coding!