JavaScript Comments

JavaScript comments allow us to leave notes that are used to explain the code we write. They make the program more readable and easier to understand for other developers.

They also make it possible to prevent code from running by commenting it out. This technique is often used when debugging.

How to comment in JavaScript?

JavaScript provides us with two ways to comment our code. Both ways are completely ignored by the JavaScript engine.

Single-line Comments //

A single-line comment is started when prefixed with //. For example:

1// This is a single-line comment
2const name = '';

However, single-line comments can also be written inline with the code like below:

1const name = '' // This is a comment

To use a single-line comment to debug your code, prefix any line with // as shown below:

1// const name = '';

Multi-line Comments /* */

Multi-line comments are created by wrapping your comment between /* and */. They are often used when leaving a long comment:

2This is a multi-line
3comment in JavaScript
6const name = '';

Or when commenting out larger parts of code while debugging:

2const name = '';
3const menu = document.getElementById('menu'); = '100%';
7const example = 'The code above is commented out';

JavaScript is a trademark of Oracle Corporation in the US. All resources shared on are owned by their respective creators.