Commenting Code for Clarity in Javascript


Use comments to explain “why” something is done, not “what” is done (which should be clear from the code itself).

Source Code

// Correct use of a loop to filter even numbers
for (let i = 0; i < numbers.length; i++) {
  // If the number is even, add it to the list
  if (numbers[i] % 2 === 0) {
    evenNumbers.push(numbers[i]);
  }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments