What is the difference between for... in and for... of statements in JavaScript
Published
12 Jan 2023
Summary
Both for...of and for...in statements iterate over lists; the values iterated on are different though, for...in returns a list of keys on the object being iterated, whereas for...of works on an iterable object and returns the result of the iterators implementation.
💡
An object is iterable if it implements the Symbol.iterator property. Some built-in types like Array, Map, and Set already have this implemented.
for...in
for...in sometimes written as for-in returns the index value for the specific loop.
let numbers = [1,2,3];numbers['four'] = 4;numbers.five = 5;
for (let i in numbers) { console.log(i);}JavaScript Code
'0''1''2''four''five'Console Output
If you want to gain access to the value when using for..in on an array, you use the index of the current loop to access from the array object.
let numbers = [1,2,3];numbers['four'] = 4;numbers.five = 5;
for (let i in numbers) { console.log(numbers[i]);}JavaScript Code
12345Console Output
for...of
for...of sometimes written as for-of gives you the value returned by the iterator implementation. This can be different depending upon how the iterater was implemented. For example, the built in iterator implementation for array iterator yields all the values in the array, ignoring non-index properties.
let numbers = [1,2,3];numbers['four'] = 4;numbers.five = 5;
for (let i of numbers) { console.log(i);}JavaScript Code
123Console Output
Using Symbol.iterator, you can create custom iterators that can be used inside of for-of Array spreads and other iterable loops. For example, you could create a new iterator that iterated over an array in reverse.
let numbers = [1,2,3];numbers['four'] = 4;numbers.five = 5;
const createCustomIterator = (inputArray) => ({ [Symbol.iterator]() { let i = inputArray.length; return { next: () => ({ value: inputArray[--i], done: i < 0 }) } }})
for (let i of createCustomIterator(numbers)) { console.log(i);}JavaScript Code
321Console Output
In this example, createCustomIterator is a function that takes an array and returns an implementation iterator. It could be used on multiple arrays to reverse iterate over them.
Similar articles

AI-First Programming Languages: What They Are, and Whether You Should Use One Yet
In the last year a genuinely new category of programming language appeared: ones where the AI, not you, is the intended author. Here's what the categories are, which projects actually have traction, and which of them I'd be willing to put in production today.
9 Aug 2026

lucide-animated: Icon Motion That Knows When to Stop
lucide-animated wraps the entire lucide-react set in small, Motion-powered animations, same names, same currentColor stroke, one extra letter in the import. I wired it into a few real spots on this site and worked out where it earns its place and where it's just noise.
2 Aug 2026

Animated Gradient Borders: A Better Loading State for AI Interfaces
Spinners and skeleton screens assume a request either hasn't returned yet or has. AI responses don't work that way, they stream in over seconds while you're already reading them. A rotating gradient border solves the "still working" signal without covering up content the user is actively looking at.
31 July 2026

Pathfinding Algorithms Explained
A live stealth pathfinding demo, and the three buckets every algorithm in the dropdown falls into: Naive, Uninformed, and Informed. Deep dives live under /algorithms.
26 July 2026
