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 reading 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 6 months ago does.

Let's go through the two ways to comment in JavaScript.

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.

Build A Hacker News Clone
Learn how to build and deploy a full-stack, production-ready application with React.js, Node.js, and MongoDB.
View Course

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 blocks of code if you desire to do so.

Thanks for reading and happy coding!