How to Add Comments in Node.js
Comments are annotations in the source code of a program that are ignored by the interpreter. As a result, they have no effect on how the code runs or the output it produces.
But they can be used to explain what your code is doing to any humans that attempt to read your code. Therefore, they can be incredibly useful to both others and your future self when you try to figure out what the code you wrote in the past does.
Let's go through the two ways to comment in Node.js.
Table Of Contents
Single-Line Comments
The first way is the single-line comment by using two forward slashes (//
).
Here's what it looks like:
// Anything after the two slashes is commented out.
Anything after the two slashes (//
) will be commented out.
You can add these comments inline with your code as well:
const tacoJohns = "Very Good!" // Comment explaining the variable
There isn't a way to end a single line comment on a line, so make sure you don't put any code after the //
hashes unless you don't want it to be seen by the interpreter.
Inline comments can be super useful, but they can make your code look messy if you use them too much.
Block Comments
The second way is called block comments. These are written with open (/*
) and close (*/
) tags.
Here's what it looks like:
/* Comment on
multiple lines */
Everything between the open and closing tags will be excluded.
Block comments will give you more options compared to the //
syntax we covered earlier. And even allow you to block out entire sections of code if you desire to do so.