Essential JavaScript

Going Further

Comments

Try running this code in the practice editor:

console.log(1 + 2); console.log(3 + 5 - 1); // console.log(55 / 11);

The result is 7. Notice how this code is almost identical to the code in a previous lesson, but with one key difference.

The // in front of that third line marks the rest of the line as a comment. JavaScript ignores comments. They are a way for developers to write notes in the code.

We can also use comments to tell JavaScript to ignore lines of code while we are working and we want to test something. You can put // in front of a line and JavaScript will ignore it.

Since JavaScript ignored the 55 / 11 expression, the last expression it ran was 3 + 5 - 1. That's why 7 came back as our result.

Multiline Comments

We can also comment out multiple lines like this:

console.log(1); /* console.log(2); console.log(3); */ console.log(4);

Everything between /* and */ is a comment.

The above code will only log 1 and 4 because the rest of the code is just a comment.

We can use this kind of comment in the middle of a line as well:

console.log(1, 2, /* 3, */ 4);

The above code won't log 3 because it has been commented out.

Learning Goals

  • I know what a code comment is

  • I can comment out a single line of code

  • I can comment out multiple lines of code

  • I can comment out code in the middle of a line

Code Editor

Click "Run Code" to execute your JavaScript in a secure sandbox and see the output below.

Console Output

// Console output will appear here...

0 output lines