In this chapter, we will learn about JavaScript comments. Comments are used to explain and clarify code, making it easier to read and maintain. They are ignored by the browser during execution. We will cover:
- Introduction to Comments
- Single-line Comments
- Multi-line Comments
- Best Practices for Using Comments
Introduction to Comments
Comments are an essential part of programming. They are used to provide explanations, clarify complex code, or temporarily disable code for debugging purposes. Comments are ignored by the browser, meaning they do not affect the execution of the program.
Single-line Comments
Single-line comments start with //
and extend to the end of the line. They are used for short explanations or to comment out a single line of code.
Syntax
// This is a single-line comment
let name = "Ramesh"; // This is also a single-line comment
Example
// Declare a variable
let age = 25;
// Display the age
console.log(age); // Output: 25
Multi-line Comments
Multi-line comments start with /*
and end with */
. They can span multiple lines and are used for longer explanations or to comment out multiple lines of code.
Syntax
/*
This is a multi-line comment.
It can span multiple lines.
*/
let name = "Ramesh";
Example
/*
This function calculates the square of a number.
It takes a single argument and returns the result.
*/
function square(number) {
return number * number;
}
// Call the function and display the result
console.log(square(5)); // Output: 25
Best Practices for Using Comments
-
Explain Why, Not What: Comments should explain why a piece of code exists or what it aims to accomplish, rather than what the code is doing. The code itself should be clear enough to explain what it is doing.
Example:
// Calculate the area of a circle const pi = 3.14; let radius = 5; let area = pi * radius * radius; console.log(area); // Output: 78.5
-
Keep Comments Up-to-Date: Ensure comments are updated when the code changes. Outdated comments can be misleading and confusing.
-
Avoid Redundant Comments: Avoid comments that simply restate what the code does.
Example:
let sum = a + b; // Adding a and b (redundant comment)
-
Use Comments for Complex Logic: Use comments to explain complex logic or calculations to make the code more understandable.
Example:
// Check if the user is an adult let age = 18; if (age >= 18) { console.log("You are an adult."); // Output: You are an adult. } else { console.log("You are a minor."); }
-
Comment Out Code for Testing: Use comments to temporarily disable code for testing purposes.
Example:
// let name = "Amit"; // console.log(name); // This code is commented out for testing let age = 25; console.log(age); // Output: 25
Example: Good Commenting Practices
// Calculate the area of a rectangle
function calculateArea(width, height) {
// Ensure width and height are positive numbers
if (width <= 0 || height <= 0) {
return 0;
}
// Calculate the area
return width * height;
}
// Display the area of a rectangle with width 5 and height 10
console.log(calculateArea(5, 10)); // Output: 50
Example: Bad Commenting Practices
// This is a function
function calcArea(w, h) {
// If width or height is less than or equal to 0, return 0
if (w <= 0 || h <= 0) {
return 0;
}
// Multiply width and height and return the result
return w * h;
}
// Output the result of calcArea with 5 and 10
console.log(calcArea(5, 10)); // 50
Conclusion
In this chapter, you learned about JavaScript comments, including single-line and multi-line comments. You also learned best practices for using comments effectively. Comments are a crucial part of writing clear, maintainable code. Understanding how to use comments properly will help you and others understand your code better.