HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

3) Learn to Crawl Before You Can Run Lesson

JavaScript Syntax: Comments

3 min to complete · By Ian Currie

In this lesson you will learn about what comments are and how to make them.

What Are Comments Used For

Comments are used in JavaScript, as in all languages, to annotate your code without having any effect on the running of it. There are two ways to comment.

Here is an example of a code snippet with some comments explaining what the code does:

let counter = 0; // This is where the counter is initialized

/*
The following code comprises a for loop,
This loop repeats 20 times,
going from i = 0 to i = 19.
In each iteration, it checks if i is even.
If it is even, then it adds to the counter.
*/

// This is the for loop
for (let i = 0; i < 20; i++) {
  if (i % 2 == 0) {
    /*This increments the counter by one*/ counter++;
  }
}

What Are the Different Types of Comments in JavaScript

Don't worry about understanding the code at this time, the important thing to take away is that there are two types of comments

Single-Line Comments

These comments begin with two forward slashes // and are used to comment on a single line of code. Anything after the // is ignored by the JavaScript interpreter.

Multi-Line Comments

These comments are enclosed between /* and */ and can span multiple lines. They are typically used to comment on multiple lines of code. Everything between /* and */ is treated as a comment and ignored.

Summary: JavaScript Syntax - Comments

  • Comments are used to annotate your code
  • Comments are ignored by the interpreter and do not affect your code

What Are the Different Types of Comments in JavaScript

  • Single-line comments which only require // at the beginning of the line
  • Multi-line comments which are enclosed between /* and */