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

Production Agentic AI: Permissions, Observability, and Kill Switches
Putting an agentic AI system into production is a different engineering problem to putting it into staging. Once it is live, the governance question shifts from "will it behave?" to "how do we know if it stops behaving, and what do we do about it?" A practical guide to production permissions, observability architecture, and kill switches for Australian engineering leaders in regulated industries.
25 Sept 2026

What Australian CTOs Should Put in the AI Governance Pack
The board is asking. The regulator is watching. The CTO is the one who has to translate "AI governance" from a policy concept into a document set that actually governs something. Here is what a credible AI governance pack looks like for an Australian organisation in 2026, and what each section needs to do to survive scrutiny.
21 Sept 2026

Governing Agentic AI: Human-in-the-Loop Patterns When Tools Can Act (MCP Edition)
Agentic AI doesn't just recommend. It acts. That changes governance from a policy question into an engineering question: which patterns actually limit blast radius, satisfy a human reviewer, and survive scrutiny from a regulator or court? A practical playbook for MCP-connected agents, with a checklist for regulated industries.
16 Sept 2026

Vercel Can Do WebSockets Now. Here's Where a Chat Room Still Breaks It.
Vercel shipped native WebSocket support in 2026, so a Function really can hold a socket open. But the connection is still a function invocation with a duration ceiling and no fan-out. Here's where that's fine for a chat app, where it isn't, and how Supabase Realtime covers the gap.
15 Aug 2026
