# Loops: for, while, for...of, for...in

> How to repeat actions in JavaScript. Learn the standard loops and the modern variations for iterating over arrays and objects.

Subject: JavaScript | Lesson 10 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/loops
Previous: [Conditionals: if, switch & ternary](https://codewithanimation.in/javascript/conditionals.md) | Next: [Iterables & the Iteration Protocol](https://codewithanimation.in/javascript/iteration-protocols.md)

## What are loops?

Loops are used to run the same block of code multiple times, usually with a slightly different value each time.

The most traditional loop is the `for` loop, which has three parts: an initializer (`let i = 0`), a condition (`i < 5`), and an afterthought (`i++`).

## Why are there so many kinds?

Different data structures require different approaches to iterate over them cleanly.

A standard `for` or `while` loop is great when you just want to repeat something 'X' amount of times.

The `for...of` loop was introduced to easily grab the *values* out of an Array or String without dealing with index math.

The `for...in` loop is designed specifically to grab the *keys* (property names) out of an Object.

## The syntax breakdown

`for (let i = 0; i < 3; i++)`: The classic loop. Good for counting.

`while (condition)`: Repeats as long as the condition is truthy. Make sure the condition eventually becomes false, or you will create an infinite loop that crashes the browser!

`for (const value of array)`: The modern, clean way to loop over an array's contents.

`for (const key in object)`: The way to loop over an object's properties.

## Common mistake: for...of vs for...in

Mixing these up is a rite of passage. If you use `for...in` on an array, it will give you the indices (`'0'`, `'1'`, `'2'`) as strings, not the actual values! Always remember: `of` is for values (Arrays), `in` is for keys (Objects).

## Tip: Breaking and Continuing

Inside any loop, you can use the `break` keyword to immediately shatter the loop and exit entirely. You can use the `continue` keyword to skip the rest of the current iteration and jump straight to the next one.

## Try it: The Loop Stepper

Step through a traditional `for` loop iteration by iteration to see exactly how the initializer, condition, body, and afterthought execute in order.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/loops

## Check yourself

### Question 1

Which loop is specifically designed to iterate over the keys (property names) of an object?

- A. for...in
- B. for...of
- C. while

Correct answer: A

The `for...in` loop iterates over all enumerable string properties of an object.

- Why not B: Incorrect. `for...of` is for iterating over values in an iterable like an Array.
- Why not C: Incorrect. A while loop just repeats based on a boolean condition.

### Question 2

What happens if you use a `for...in` loop on an array?

- A. It iterates over the array's index numbers as strings ('0', '1', '2').
- B. It iterates over the actual values in the array.
- C. It throws a TypeError.

Correct answer: A

Arrays are fundamentally just objects under the hood where the keys are numbers. So `for...in` will return those index keys as strings.

- Why not B: Incorrect. That is what `for...of` does.
- Why not C: Incorrect. Arrays are technically objects, so the loop runs, but behaves unexpectedly.

### Question 3

In a standard `for (let i = 0; i < 5; i++)` loop, when does `i++` run?

- A. At the end of each iteration, after the code block runs.
- B. At the beginning of each iteration, before the code block runs.
- C. Only once, when the loop starts.

Correct answer: A

The third part of a for loop (the afterthought) executes at the very end of every single iteration, right before the condition is checked again for the next round.

- Why not B: Incorrect. The condition is checked at the beginning, but the afterthought runs at the end.
- Why not C: Incorrect. The initializer (`let i = 0`) only runs once.

## Remember this

- A standard `for` loop has three parts: initializer, condition, and afterthought.
- Use `for...of` to iterate over the values of an Array or String.
- Use `for...in` to iterate over the keys of an Object.
- `break` exits the loop entirely; `continue` skips to the next iteration.
