Loading topic
for...in and for...of. They differ by one word, but they answer different questions. for...in asks "what are the property keys of this object?" for...of asks "what values does this collection produce?" Most bugs with these loops come from2 min readlet and const, and how to read object properties with dot and bracket notation. It covers the basic shape of a for...in loop and what the loop variable holds. Later chapters cover which properties the loop skips, inherited keys and ke2 min readfor...in loop. It explains why some properties never show up in a for...in loop even though you can read them.2 min readfor...in visits enumerable string keys, and that you have a rough idea of prototypes: every object can link to another object and borrow its properties.2 min readfor...in walks an object's enumerable keys and how inherited properties show up in that walk. The question here is the order in which the keys arrive. The order is not random. The specification defines it, and for...in, Object.keys, Object.values and `Object2 min readlet and const, can work with arrays and objects, and have read the earlier chapters on for...in. Here the focus moves from property keys to values.2 min readfor...of syntax from the previous chapter, plus arrays, objects and functions. Here we look at which built-in values for...of accepts and what each one yields.1 min readfor...of walks the values of an iterable and that strings are one of the built-in iterables. It also helps to know that JavaScript stores strings as sequences of 16-bit units.2 min readfor...of syntax, the built-in iterables (arrays, strings, Maps, Sets), and array destructuring such as const [a, b] = pair.2 min readfor...of and destructuring in the loop head, and that you know what enumerable properties are. Plain objects cannot go straight into for...of. Three static methods turn an object into an array, and arrays are iterable.2 min readfor...in visits enumerable string keys, including inherited ones, and how for...of visits the values of an iterable. Here we put the two loops side by side on arrays. You will see why for...in gives surprising results there.2 min readlet, const and var differ in scope, and that you can write arrow functions. It also assumes you understand that a closure is a function that remembers the variables around it.2 min readfor...of works on arrays, strings and Maps, and that you can read basic object and function syntax. Here you will see what for...of does each time it runs.2 min readfor...of syntax and the iterator protocol from the previous chapter: for...of gets an iterator from Symbol.iterator and calls next() until done is true. It also assumes basic familiarity with generator functions.2 min readSymbol.iterator returns an object with next()), and basic function* generator syntax.1 min readfor...of over arrays, Maps and Sets, and for...in over objects. It covers what happens when the loop body adds or removes items from the collection being looped over.2 min readasync/await, and that you have read the earlier chapters on the iterable protocol and generators. A plain for...of loop pulls each value as soon as it asks for one. Some sources can't work that way, such as a network stream, paged API results or file 2 min readfor...in, for...of, Object.entries and early exit with break. It brings those pieces together into one decision: which loop fits the task in front of you.2 min read