Home JavaScript JavaScript Comments

JavaScript Comments

Beginner ⏱ 5 min read Updated: Jul 2026
  • JavaScript comments are used to add notes or explanations inside code
  • Comments are ignored by the JavaScript engine during execution
  • They help developers understand and maintain code easily
  • JavaScript supports two types of comments: single-line and multi-line comments

What are JavaScript Comments?

JavaScript comments are lines of text written inside a program to explain what the code does. Comments are not executed by the browser or JavaScript engine, which means they do not affect the output of the program.

Developers use comments to make code easier to read, understand, and maintain. They are also useful when debugging programs or temporarily disabling a piece of code.

Why Use Comments in JavaScript?

Code Explanation

Comments explain the purpose and working of code.

Better Readability

They make programs easier for other developers to understand.

Debugging

Comments can temporarily disable code while testing.

Documentation

They help maintain proper documentation inside programs.

Types of JavaScript Comments

JavaScript provides two types of comments:

  • Single-line comments
  • Multi-line comments

1. JavaScript Single-Line Comments

Single-line comments are used when you want to write a comment on only one line. They start with two forward slashes //.

Example

// This is a single-line comment
let name = "Shivam";
console.log(name);
        
Result
Shivam

Example Explained

//

It tells JavaScript that the following text is a comment.

let

It creates a variable to store a value.

console.log()

It displays output in the browser console.

2. JavaScript Multi-Line Comments

Multi-line comments are used when comments need multiple lines. They start with /* and end with */.

Example

/*

This is a multi-line comment.
It can contain multiple lines of text.

*/
let age = 21;
console.log(age);
        
Result
21

Using Comments to Disable Code

Developers can use comments to temporarily stop a line of code from executing. This is helpful while testing and debugging.

Example

// console.log("This will not run");

console.log("Hello JavaScript");
        
Result
Hello JavaScript

Best Practices for JavaScript Comments

💡
Tips: Write meaningful comments, avoid unnecessary comments, and keep comments updated whenever you modify your code.
💡 Note: Comments do not increase or decrease the performance of JavaScript programs because they are ignored during execution.