Commenting JavaScript Code

This tutorial is part of a web programming course for people who have no programming experience.


Good programmers put notes in their code for themselves and for other programmers. Comments can be used as reminders, they can explain bits of code that may be tricky to understand, and they can actually be used to produce formal documentation for a program.

Comments are not code, and the browser will ignore them when it runs your program. You have to use special characters so that the browser can tell the difference between code and comments.

JavaScript allows you to comment your code in two different ways.

Single Line Comments

To comment out a single line, just start the line with two forward slashes //.

// this is a  comment

Multi-Line Comments

If you need to write a long comment that will require more than one line, you can put /* before the comment, and */ after it.

/*
This is a comment that spans 
multiple lines
*/

There is one other great use for comments. If you have code that you may not want to keep, but aren't sure yet, you can simply comment it out so that the browser ignores. This is a better option than deleting it, because you find that you do need it, you can simply remove the comments without having to re-write all the code.

// the following code will be ignored by the browser because it's commented out
/*
alert("blah");
console.log("blah");
*/
NEXT LESSON: JavaScript IF Statements