Back to JavaScript Fundamentals

The power of JavaScript comments - How to write comments in JavaScript

Comments are a powerful features in any programming language, and JavaScript is no exception. This guide will show you how to write comments in JavaScript, including single-line and multi-line comments.
3 min readLogan FordLogan Ford
Difference between const, var and let in JavaScript

What are comments in JavaScript?

Comments are a way for developers to leave little snippets of information about the code that they write. This can be used to explain what the code does, how it works, or even just to leave a note for yourself or others who will be working on the code.


When working with a team of developers, it's important to leave comments so that others know what the code does. This is especially important when working on large projects with many developers.


There are a few different types of comments in JavaScript, but they all serve the same purpose. Let's take a look at the different types of comments in JavaScript.


Single-line comments

Single-line comments are the most basic type of comment in JavaScript. They are denoted by a double forward slash (//) followed by the comment.


1// This is a single-line comment

Depending on your code editor, comments may be highlighted in a different color. For example, in the default VSCode theme, single-line comments are highlighted in green.


Multi-line comments

Multi-line comments are a way to comment a large block of code. They are denoted by a forward slash and asterisk (/*) at the beginning of the comment, and an asterisk and forward slash (*/) at the end of the comment.


1/*
2* This is a multi-line comment
3* This is the second line of the comment
4* This is the third line of the comment
5*/

Multi-line comments are especially useful when documenting functions. You can use them to explain what the function does, the parameters it takes, and what it returns.


1/*
2* This function takes two numbers and returns the sum of the two numbers
3* @param {number} a - The first number
4* @param {number} b - The second number
5* @returns {number} The sum of the two numbers
6*/
7function add(a, b) {
8 return a + b;
9}

When to use comments

Comments are a powerful feature in JavaScript, and in an ideal world, you would use them everywhere. The more comments you have, the easier it is to understand the code.


However, comments can be a bit of a pain to maintain. They can become outdated, and they can make the code harder to read. So you must ensure that the along with code changes, the comments are updated when necessary.


When not to use comments

Comments are not a good idea for obvious things like variable names. For example, you should not use comments to explain what a variable is.


1// This is a comment that explains what the variable is
2let name = 'John'; // This is a comment that explains what the variable is

In this case, the variable name is quite obvious, so it doesn't need a comment.


Summary

Comments are a powerful feature in JavaScript, and in an ideal world, you would use them everywhere. The more comments you have, the easier it is to understand the code.


However, comments can be a bit of a pain to maintain. They can become outdated, and they can make the code harder to read. So you must ensure that the along with code changes, the comments are updated when necessary.


Looking to learn the basics of JavaScript?

If you're looking to learn the basics of JavaScript, check out TechBlitz's JavaScript Fundamentals course.


This course is designed to teach you the basics of JavaScript, including variables, data types, functions, and more. You can check it out for free, simply sign up for a free account here!.


Land your dream tech job faster

Join 810+ developers who are accelerating their coding skills with TechBlitz.

Share this article