Introduction
In this tutorial, you will learn how to comment in JavaScript code. JavaScript code that is ignored by the JavaScript engine when the code is executed.
In JavaScript, comments are used to explain code, improve readability, and make maintenance easier. They are ignored by the JavaScript engine, meaning they donβt affect how the program runs.
It is used to explain code, make notes, or temporarily disable certain parts of the code.
Comments are helpful for both the developer writing the code and others who might read or maintain the code in the future.
What are JavaScript Comments?
JavaScript comments are lines of text added to the code that are not executed by the browser or JavaScript engine. They are used to describe what the code is doing or to leave notes for developers.
- Single-line comments start with //
- Multi-line comments start with /* and end with */
- Comments can be placed anywhere in the code.
- They are ignored during execution
Why are Comments Used in JavaScript?
Comments play an important role in development for several reasons.
1. Improve Readability
Comments help developers understand the purpose of code quickly.
2. Documentation
They act as inline documentation for functions, variables, and logic.
3. Debugging
You can temporarily disable code using comments.
4. Collaboration
When working in a team, comments help other developers understand your code.
5. Maintenance
Makes it easier to update or modify code in the future.
Types of JavaScript Comments
There are two types of comments
Single-Line Comments
It is used for a single line. It is used to add short notes or explanations on one line.
They start with // and extend to the end of the line.
Syntax:
// This is a single-line comment
Example:
// This is a single-line comment
let name = 'John'; // Variable name is assigned the value John
Multi-Line Comments
It is used for Multi-Line Comments. It is used for longer explanations or for commenting out multiple lines of code.
They start with /* and end with */
Syntax:
/*
This is a multi-line comment
It can span multiple lines
*/
Example:
/*
This is a multi-line comment.
It can span multiple lines.
It is useful for longer descriptions or temporarily disabling blocks of code.
*/
let x = 10;
Real Life Example of Comments
In this example, you will see a function to calculate a discount.
// Function to calculate discount
function calculateDiscount(price, discount) {
// Ensure discount is valid
if (discount > 0 && discount <= 100) {
let finalPrice = price - (price * discount / 100);
return finalPrice;
} else {
// Return original price if discount is invalid
return price;
}
}
console.log(calculateDiscount(1000, 10));
Common Mistakes in JavaScript Comments
You will see some common mistakes in JavaScript Comments
1. Outdated Comments
// adds two numbers
let result = a - b; // incorrect comment
2. Overusing Comments
Too many comments can reduce readability.
3. Nested Comments (Not Allowed)
/* outer comment
/* inner comment */ // Error
*/
Advanced: JSDoc Comments
JSDoc is a standard for documenting JavaScript code.
Example:
/**
* Adds two numbers
* @param {number} a
* @param {number} b
* @returns {number}
*/
function add(a, b) {
return a + b;
}
Benefits:
- Better documentation
- IDE support
- Useful for large projects
JavaScript Comments vs Documentation
| Feature | Comments | Documentation |
|---|---|---|
| Purpose | Explain code | Explain system |
| Scope | Inside code | External or detailed |
| Use | Developers | Users & developers |
JavaScript Comments β Interview Questions
Q 1: What are comments in JavaScript?
Q 2: Single-line comment syntax?
Q 3: Multi-line comment syntax?
Q 4: Why use comments?
Q 5: Can comments affect performance?
JavaScript Comments β Objective Questions (MCQs)
Q1. Which symbol is used for single-line comments?
Q2. Multi-line comments start with ______.
Q3. Multi-line comments end with ______.
Q4. Comments are used to ______.
Q5. JavaScript comments are ignored by the ______.
Conclusion
JavaScript comments are a simple yet powerful tool that improves code readability, maintainability, and collaboration. Whether you're a beginner or an experienced developer, using comments effectively can make your code easier to understand and debug.
With the help of Comments, you can write clean and professional JavaScript code that others can easily understand.