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 = 'Java5cript.com';

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

1const name = 'Java5cript.com' // This is a comment

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

1// const name = 'Java5cript.com';

Multi-line Comments /* */

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

1/*
2This is a multi-line
3comment in JavaScript
4*/
5
6const name = 'Java5cript.com';

Or when commenting out larger parts of code while debugging:

1/*
2const name = 'Java5cript.com';
3const menu = document.getElementById('menu');
4menu.style.width = '100%';
5*/
6
7const example = 'The code above is commented out';

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