Skip to content

Try our new Crash Courses!

Buy one of our new Crash Courses, now hosted on Podia.

Comments – JavaScript

Summary

Single line comments syntax:

// Single line comment

Multi-line comments syntax:

/*
  Multi-line
  comments
  in JavaScript
*/

Details

Comments in code allow you to write notes to yourself about the code you’ve written. The text inside a comment will not show up on the actual web page.

As you can see from the examples above, comments can span a single line or multiple lines.

Disabling code using comments

Comments can also be used to temporarily disable sections of code, so you can see how your web page works without executing certain lines of code. Here’s an example:

// console.log("This statement won't print out to the console when it's in a comment");

If you decide that you don’t need the code that you’ve commented out, it’s best to delete the code rather than leave it in so you don’t end up with a bunch of commented out code in your JavaScript file.

VS Code Tip

To quickly create a comment in VS Code, press Ctrl + / on Windows or Cmd + / on macOS. This will create a new comment on a blank line or comment out the line your cursor is on if there is already code on it.

Exercises

Try the following statements in the console:

// this is a comment

References

JavaScript comments on MDN

Back to: JavaScript Reference > JS Basic Syntax