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.