Understanding Comments and Debugging Code in JavaScript
This article is a complementary resource to the Learn JavaScript Basics course.
This article is a complementary resource to the Learn JavaScript Basics course.
Comments are an essential part of programming—used for explaining code, clarifying logic, and improving readability.
You can turn any line into a comment by placing the //
symbol at the beginning. For example,
// Display "Hello, World!"
console.log("Hello, World!");
Here, // Display "Hello, World!"
is a comment.
JavaScript ignores comments during execution. Thus, the above code is equivalent to:
console.log("Hello, World!");
Comments are vital for understanding the "why" behind your code, especially when revisiting it after some time or collaborating with others.
Without comments:
let score = 100;
score -= 10;
console.log(score);
It's unclear why score starts at 100 or why it's being reduced. Now imagine if the code had comments:
With comments:
// Initial player score
let score = 100;
// Deduct points for a mistake
score -= 10;
console.log(score);
With comments, the purpose is immediately clear—score is the player's starting value, and the reduction simulates losing points.
This clarity is beneficial both for you and for anyone else who might work on the code.
1. Single-Line Comments:
Single-line comments begin with //
and span a single line.
// Initialize the counter
let counter = 0;
2. Multi-Line Comments:
Multi-line comments are enclosed between /*
and */
.
They can span multiple lines and are ideal for longer explanations or temporarily disabling large blocks of code.
/*
This function calculates the sum of two numbers.
Takes two arguments: a and b.
Returns their sum.
*/
function add(a, b) {
return a + b;
}
Good comments make your code easy to understand and maintain. Here's how to keep them effective:
Keep comments brief and to the point.
Bad Example:
// This section of code checks if the user input is a valid number by
// attempting to convert it to an integer, and if that fails, it catches
// the error and prompts the user again until a valid number is entered.
let userInput = prompt("Enter a number:");
try {
let num = parseInt(userInput, 10);
} catch (error) {
console.log("That's not a valid number.");
}
The explanation in this comment is too detailed for such a simple operation.
Good Example:
// Try to convert input to a number. If it fails, print an error.
let userInput = prompt("Enter a number:");
try {
let num = parseInt(userInput, 10);
} catch (error) {
console.log("That's not a valid number.");
}
Comments should explain the why behind the code, not restate the obvious.
Bad Example:
// Increment x by 1
x = x + 1;
This is obvious! The code already explains what's happening, so the comment adds no value.
Good Example:
// Increment x to move to the next step in the process
x = x + 1;
This explains why the code is incrementing x
rather than simply stating the action.
Avoid writing comments that repeat what the code already makes clear.
Bad Example:
let x = 5; // Set x to 5
The comment is redundant because the code itself clearly shows what's happening.
Good Example:
let x = 5;
It's common to add comments while developing, especially for things that need attention or future improvement. But always remember to clean up "work-in-progress" comments once the code is finalized.
Bad Example:
// TODO: Optimize this loop for better performance
for (let i = 0; i < 1000000; i++) {
processItem(i);
}
The "TODO" comment is useful during development but should be removed or revised when the code is finalized. Leaving it in the final code can be misleading.
Good Example:
// Optimized the loop to improve performance by reducing redundant calls
for (let i = 0; i < 1000000; i++) {
optimizedProcessItem(i);
}
This comment now reflects the completed task and explains the optimization made.
Well-written comments enhance code readability and make it easier to maintain and debug.