How to comment in JavaScript

Using comments in code can be useful for a variety of reasons. Maybe you work in a team or you are creating a reusable script. Regardless of your development needs, comments are a great way to make your code maintainable.

Comments in JavaScript can be done in two ways:

Single Line JavaScript Comment

To comment a single line of JavaScript you begin the comment with two forward slashes “//”. For example:

// declare a variable for color
var color = "red";

Multiple Line JavaScript Comment

To comment multiple lines you begin the comment with “/*” and end with */. For example:

/* a multi line comment to say that
   my favorite color is not red
   it is blue
*/
var color = "blue";