LearnFrenzy
Sign in Take a free test

Variables: var, let and const · Find the Error · Question 8

Error: const as a for-loop counter Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
for (const i = 0; i < 3; i++) {
  console.log("Row", i);
}
  • A It prints Row 0, then TypeError: i++ cannot reassign the const i – use let Correct
  • B SyntaxError before anything runs
  • C It prints Row 0, Row 1 and Row 2
  • D It loops forever printing Row 0
Correct Answer: Option A

The first iteration runs normally; the update expression i++ then throws "Assignment to constant variable". A classic for loop needs let.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links