Loops & Iteration

JavaScript Fundamentals13 pts

Back

Repeating Code with Loops

Loops execute code multiple times. JavaScript has for loops, while loops, and array iteration methods like forEach.

Use for loops when you know how many times to iterate. Use while loops when the number of iterations depends on a condition.

Output:
  • Create function countToN
  • countToN(5) should return [1,2,3,4,5]
  • Array length should match n
Which loop is best for iterating over array elements?
Select an answer
Modern JavaScript favors array methods (map, filter, forEach) over traditional for loops for better readability.