Skip to content

Iterable vs Enumerable

JavaScript has two loops that look alike but answer different questions. for...in goes through an object’s enumerable properties and gives you their keys. for...of goes through an iterable object and gives you the values that its iterator produces.

An enumerable property is one whose enumerable flag is true, which is the default for any property created by assignment or in an object literal. An iterable is an object with a Symbol.iterator method, which arrays, strings, maps, sets and typed arrays all have, while plain objects do not.

An array is both, so the two loops give different results on the same array:

const temps = [12, 15, 9];
temps.unit = 'celsius';
for (const key in temps) {
console.log(key);
}
for (const value of temps) {
console.log(value);
}

This prints:

0
1
2
unit
12
15
9

for...in lists the indexes as strings, and it also lists unit, because that is an enumerable property too. for...of uses the array’s iterator, which yields only the elements, so unit does not appear. This is why for...in is a poor choice for arrays.

A property defined with enumerable: false still exists and can be read, but for...in and Object.keys() skip it:

const book = { title: 'Dune', pages: 412 };
Object.defineProperty(book, 'isbn', { value: '978-0441013593', enumerable: false });
for (const key in book) {
console.log(key);
}
console.log(Object.keys(book));
console.log(book.isbn);

This prints:

title
pages
[ 'title', 'pages' ]
978-0441013593

Built-in methods such as toString are non-enumerable for the same reason, so they stay out of your loops. Property Descriptors covers the flags in more detail.

for...in also visits enumerable properties that an object inherits through its prototype, whereas Object.keys() returns only the object’s own properties:

const defaults = { currency: 'GBP' };
const basket = Object.create(defaults);
basket.items = 3;
for (const key in basket) {
console.log(key);
}
console.log(Object.keys(basket));

This prints:

items
currency
[ 'items' ]

A plain object has no Symbol.iterator method, so for...of throws a TypeError. To loop over its values, turn it into an array first with Object.keys(), Object.values() or Object.entries():

const book = { title: 'Dune', pages: 412 };
try {
for (const value of book) {
console.log(value);
}
} catch (err) {
console.log(err.message);
}
for (const [key, value] of Object.entries(book)) {
console.log(`${key}: ${value}`);
}

This prints:

book is not iterable
title: Dune
pages: 412

Strings, maps and sets work with for...of as well. A string yields its characters, and a map yields [key, value] pairs. You can check for an iterator by looking for Symbol.iterator:

for (const ch of 'tea') {
console.log(ch);
}
const stock = new Map([['apples', 6], ['pears', 2]]);
for (const [fruit, count] of stock) {
console.log(fruit, count);
}
console.log(typeof [][Symbol.iterator]);
console.log(typeof {}[Symbol.iterator]);

This prints:

t
e
a
apples 6
pears 2
function
undefined

In a browser, DOM collections such as the NodeList returned by querySelectorAll() are iterable too. To make your own objects iterable, see Custom Iterators and for…of Loops.

Steve Griffith covers this topic in his video Iterable vs Enumerable in JavaScript.