JS Comments
JavaScript comments are a crucial part of writing clean, readable, and maintainable code. They allow you to explain your code's logic, provide context for other developers (or your future self!), and even temporarily disable sections of code without deleting them. Think of them as notes you leave for yourself, but they’re essential for collaborative projects and long-term codebases. Without comments, your code can become incredibly difficult to understand, leading to errors and frustration.
Here are a few practice exercises you can try in the live editor to solidify your understanding of JavaScript comments:
-
Explain a Function: Write a function that calculates the area of a rectangle. Then, add 50 comments explaining each step of the function's logic. Focus on what each comment is doing rather than just stating the result.
-
Comment Out a Section: Create a simple JavaScript function that prints "Hello, world!" to the console. Then, comment out all the lines of code except for the
console.log("Hello, world!");line. After commenting out the rest, re-enable the code and observe the output. This will help you understand how comments affect the execution flow. -
Add a Comment to a Variable: Create a JavaScript variable named
myNumberand assign it the value 10. Then, add a comment explaining what the variable represents. Finally, add a comment explaining how you would use the variable later in your code.
Let's dive into some practical examples of how to use JavaScript comments effectively:
1. Explaining a Function
// This function calculates the area of a rectangle.
function calculateArea(length, width) {
// Calculate the area by multiplying length and width.
const area = length * width;
// Return the calculated area.
return area;
}
// Example usage:
const myRectangle = calculateArea(5, 10);
console.log("The area of the rectangle is: " + myRectangle);
In this example, the first comment // This function calculates the area of a rectangle. explains the purpose of the function. The second comment const area = length * width; explains the calculation being performed. The third comment return area; explains the return value. Good comments make the code self-documenting.
2. Commenting Out a Section
// This function prints "Hello, world!" to the console.
function printHelloWorld() {
console.log("Hello, world!");
}
// Comment out all other code except for this line.
// console.log("This is a comment.");
// Re-enable the code and observe the output.
printHelloWorld();
This example demonstrates how to comment out a section of code. By commenting out the console.log statement, you effectively disable the output. The console.log("This is a comment."); line is still present, but it's not executed. This is useful for isolating specific parts of your code for testing or debugging.
3. Adding a Comment to a Variable
// This variable stores the user's name.
let userName = "Alice";
// This comment explains what the variable represents.
// It's a simple variable that holds the user's name.
let userName = "Alice";
Adding a comment to a variable clarifies its purpose. This is especially helpful when you're working with complex data structures or algorithms. It makes the code easier to understand for yourself and others.
4. Adding a Comment to a Variable
// This variable stores the user's name.
let userName = "Alice";
// This comment explains what the variable represents.
// It's a simple variable that holds the user's name.
let userName = "Alice";
Adding a comment to a variable is a good practice. It helps to clarify the purpose of the variable and makes the code more readable.
💡 Tip: Don't over-comment. Comments should explain why you're doing something, not what you're doing. Keep comments concise and focused. Use comments to clarify complex logic, and to explain the intent behind your code. Good comments are a sign of thoughtful code, not a sign of sloppy coding.
🖥️ Try It Yourself
Here are a few more practice exercises to test your understanding:
-
Explain a Loop: Write a JavaScript function that iterates through an array of numbers and prints each number to the console. Add comments explaining the loop's logic.
-
Add a Comment to a Conditional Statement: Create a JavaScript function that checks if a number is even or odd. Add a comment explaining the conditional statement's purpose.
-
Add a Comment to a Function: Create a JavaScript function that takes a string as input and returns the reversed string. Add a comment explaining the function's functionality.

