Table of contents
1.
Introduction
2.
Single-Line Comments
2.1.
Syntax
3.
Multi-Line Comments
3.1.
Syntax
4.
JavaScript Comments in HTML
5.
Advantages of JavaScript Comments
6.
JavaScript Comments to Prevent Execution
7.
Frequently Asked Questions
7.1.
What is the difference between single-line and multi-line comments in JavaScript?
7.2.
Can I use JavaScript comments in HTML?
7.3.
Why should I use comments in my code?
8.
Conclusion
Last Updated: Jan 19, 2025
Easy

JavaScript Comments

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript comments are a powerful tool for developers to make their code more readable and maintainable. Comments are small points that are ignored by the computer when running the code, but they help programmers understand the purpose & function of different parts of the code. 

JavaScript Comments

In this article, we will discuss about the different types of comments in JavaScript, their syntax, and their practical uses. 

Single-Line Comments

Single-line comments in JavaScript start with //. These are used to add short explanations or notes about the code. They are ignored by the JavaScript engine during execution.

Syntax

// This is a single-line comment
console.log("Hello, World!"); // Prints Hello, World!


Example:

// Declare a variable to store the user's name
let userName = "John";


// Display the user's name in the console
console.log("User Name:", userName);
You can also try this code with Online Javascript Compiler
Run Code


Output:

User Name: John


Explanation:

  • The comment // Declare a variable to store the user's name describes the purpose of the userName variable.
     
  • Comments help other developers (or your future self) understand the code quickly.

Multi-Line Comments

Multi-line comments are written between /* and */. These are useful when you need to write detailed explanations or temporarily block out a chunk of code during debugging.

Syntax

/*
  This is a multi-line comment.
  It can span multiple lines.
*/
console.log("JavaScript is fun!");


Example:

/*
  The following code calculates the sum of two numbers
  and prints the result to the console.
*/
let number1 = 10;
let number2 = 20;
let sum = number1 + number2;


console.log("The sum is:", sum);
You can also try this code with Online Javascript Compiler
Run Code


Output:

The sum is: 30


Explanation:

  • Multi-line comments provide detailed context about the functionality of the code.
     
  • These are particularly helpful when working on collaborative projects.

JavaScript Comments in HTML

You can include JavaScript code inside HTML files using <script> tags, and comments can be added directly within the script block. These comments function the same way as in JavaScript files.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Comments</title>
</head>
<body>
  <script>
    // Single-line comment inside HTML
    console.log("This is a single-line comment example.");


    /*
      Multi-line comment inside HTML
      This section explains multi-line comments
    */
    console.log("This is a multi-line comment example.");
  </script>
</body>
</html>


Output:

This is a single-line comment example.
This is a multi-line comment example.


Explanation:

  • Comments in the <script> block follow JavaScript conventions.
     
  • This allows developers to document their code within HTML files effectively.

Advantages of JavaScript Comments

  • They make code easier to understand. Adding comments helps explain what different parts of the code do. This makes it simple for you or others to understand the purpose and logic, especially when revisiting old code.
     
  • Comments help in teamwork. When working with others, comments share key details about how the code works. This makes dividing tasks and staying aligned much easier.
     
  • You can disable sections of code using comments. This lets you temporarily remove code without deleting it, which is helpful for debugging or testing different ideas.
     
  • Comments can generate documentation. Some tools use formatted comments to create documentation, keeping records of your code’s functionality up to date.

JavaScript Comments to Prevent Execution

Sometimes, you may want to test specific parts of your code while temporarily disabling others. You can use comments to "comment out" code, preventing it from executing.

Example:

// This part of the code is disabled
// let greeting = "Hello";
// console.log(greeting);


// Only this line will execute
console.log("Goodbye");
You can also try this code with Online Javascript Compiler
Run Code


Output:

Goodbye


Explanation:

  • The lines starting with // are ignored by JavaScript, so only the console.log("Goodbye") statement executes.
     
  • This technique is useful when testing or debugging code.

 

Another Example:

/*
let a = 5;
let b = 10;
console.log(a + b); // This part of the code is commented out
*/
console.log("Testing complete");
You can also try this code with Online Javascript Compiler
Run Code


Output:

Testing complete


Explanation:

  • The multi-line comment prevents the block of code from executing, allowing you to test other parts of the program.

Frequently Asked Questions

What is the difference between single-line and multi-line comments in JavaScript?

Single-line comments (//) are used for short notes, while multi-line comments (/* */) allow for detailed explanations spanning multiple lines.

Can I use JavaScript comments in HTML?

Yes, JavaScript comments work inside <script> tags in HTML files.

Why should I use comments in my code?

Comments improve code readability, simplify debugging, and make it easier for teams to collaborate effectively.

Conclusion

In this article, we discussed JavaScript comments, including single-line and multi-line comments, their usage in HTML, and how they improve code readability and debugging. Comments are invaluable for documenting your code and enhancing collaboration, especially for beginners and professionals alike.

Live masterclass