HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Comments

In JavaScript, comments are used to add explanatory notes within the code.
They are ignored by the JavaScript engine during execution and are solely for the benefit of developers.
Comments can help improve code readability, explain complex logic, and provide context for future developers working on the codebase.


JavaScript supports two types of comments:

Single-line comments:

These comments start with // and continue until the end of the line. They are typically used for short explanations or annotations.

Example

// This is a single-line comment
let x = 5; // Initializing a variable

Multi-line comments:

These comments start with /* and end with */. They can span multiple lines and are useful for longer explanations or commenting out blocks of code.

Example

  /*
This is a multi-line comment.
It can span across multiple lines.
*/
let y = 10;

/*
This is another way to comment out
multiple lines of code.
*/
/*
let z = 15;
console.log(z);
*/

Conditional commenting:

You can use a condition in a single-line comment to make it conditional, so the line will be executed or not based on the condition.

Example

 // if (false) console.log("This line won't be executed");
console.log("This line will be executed");
You can click on above box to edit the code and run again.

Output