# For Of Loops

The `for...of` statement creates a loop that iterates over iterable objects (`Array`, `Map`, `Set`, `arguments`, etc.). `for...of` is particularly useful for iterating over other kinds of collections. cannot iterate over objects, so `for...in` provides the best way to iterate over an object. For of loops were introduced in ES6. They're excellent for iterating over arrays.

## For in versus For of

For in loops are best used to iterate over objects, where for of loops are great for iterating over arrays. Take the below example. I want an array of cat names, and I want to console each name and "says meow". If I try and do it in a for in loop, I won't get what I'm looking for. Take the below code.

```javascript
// cat example

var catArray = ['tabby', 'british shorthair', 'burmese', 'maine coon', 'rag doll'];

for (var cat in catArray){
  console.log(cat, 'says meow');
}

// this results in 0 says meow
// 1 says meow
// 2 says meow
// 3 says meow
// 4 says meow
```

Notice that using a `for...in` loop results in the *keys* being printed out. So the array indexes, or keys are printed instead of the value we want.

```javascript
// cat example

var catArray = ['tabby', 'british shorthair', 'burmese', 'maine coon', 'rag doll'];

for (var cat of catArray){
  console.log(cat, 'says meow');
}

// this results in:
// tabby says meow
// british shorthair says meow
// burmese says meow
// maine coon says meow
// rag doll says meow
```

When we use `for...of`, we get the value of the items in our array printed out. This is usually what you want when iterating over an array, so for now keep in your mind that "for of" is useful for things like arrays, and "for in" is useful for objects.

```javascript
// for of loops

for (var i /*variable section*/ of obj /*object section*/) {
  console.log(i) /*statement*/
}

// Practice
var array1 = [3,5,7];

// For In Loop - note that it only logs out the KEYs in the key/value pairs
for (var a in array1) {
  console.log(a); // '0', '1', '2'
}

// For Of Loop - it logs the values
for (var a of array1) {
  console.log(a); // '3', '5', '7'
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eleven-fifty-academy.gitbook.io/javascript-101-fundamentals/1-javascript-fundamentals/1-js-fundamentals/3-loops/for-of-loops.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
