for in loops over enumerable property names of an object.

for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.

In your example, the array iterator does yield all the values in the array (ignoring non-index properties).

Answer from Bergi on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for...of
for...of - JavaScript | MDN - MDN Web Docs
A for...of loop operates on the values sourced from an iterable one by one in sequential order. Each operation of the loop on a value is called an iteration, and the loop is said to iterate over the iterable.
Discussions

javascript - For-Of Loop vs. For Loop - Stack Overflow
I'd always use for..of loops, as they are faster to write and easier to read. Then if you can't achieve something with it (e.g. iterating backwards, or only every second element), then fall back to for. Just my 2cents though. ... In Javascript, Objects (among these are Arrays) store properties ... More on stackoverflow.com
🌐 stackoverflow.com
Difference between classic for loop and for...of loop in JS?
Hi all. :grinning: I’ve learned how to use the classic for loop and the for…of loop. And I was wondering about the difference between classic for loop and the for…of loop. :stuck_out_tongue: For example with a classic … More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
September 17, 2020
[AskJS] Difference between For Loops
Some differences include: for loops for (let i = 0; i < arr.length; i++) { // loop code } User defined loop conditions Allows breaking (and continuing) Supports await/yield for surrounding scope Standard usage for arrays gives you index only, not value Tends to be more complicated than other loops (less readable, more error prone) But also most flexible in how looping is defined for of loops for (const element of arr) { // loop code } Loops through iterables only Allows breaking (and continuing) Supports await/yield for surrounding scope Standard usage for arrays gives you value only, not index Other helpers needed for specific kinds of iteration, like array.entries() to get indices and values, or Object.keys() for going through keys of objects For arrays, loops through all values, even holes when arrays are sparse for in loops for (const key in obj) { // loop code } Loops through any value Iterates through value keys, both own and inherited (Object.keys(), by comparison, only provides own keys, not inherited) Allows breaking (and continuing) Supports await/yield for surrounding scope For arrays does not include holes when arrays are sparse (not recommended for looping through arrays) forEach loops arr.forEach(function (element, index, arr) { // loop code }) Only usable for collections that have this method (arrays, typed arrays, and some others like NodeLists from DOM API) Uses a callback function rather than looping through a code block Does not allow breaking Does not support await/yield for surrounding scope (though you can await and yield in the callback itself, it doesn't affect the looping) Gives you value, index, and collection being looped for each iteration For arrays does not include holes when arrays are sparse Often considered the most readable, especially when reusing callback functions (arr.forEach(doThing)) though can also be least performant More on reddit.com
🌐 r/javascript
61
97
November 26, 2021
FOR loop vs FOR IN / FOR OF loops
It's not problematic at all. If you need to iterate over an array or an iterable, for...of/for...in should be preferred. Use the regular for when you actually need a number or some irregular loop, which, I'd say, is not that common in day-to-day development. More on reddit.com
🌐 r/learnjavascript
3
3
January 17, 2022
🌐
DEV Community
dev.to › laurieontech › let-s-loop-for-in-vs-for-of-4loh
Let's loop - for...in vs for...of - DEV Community
July 17, 2019 - #javascript #webdev #productivity #learning · A bit ago I was working with Object.entries and wasn't seeing the functionality I expected. I kept staring, and staring and finally realized I was using "for in" instead of "for of". And that got me thinking I should write a post to talk about the differences. So here we are! for...in and for...of are substitutions for a traditional for loop.
🌐
Codingem
codingem.com › home › javascript for…of vs for…in loops (a complete guide)
JavaScript for...of vs for...in Loops (A Complete Guide)
July 10, 2025 - It will not include properties ... JavaScript, the for...in loop is used to iterate over the enumerable properties of an object, while the for...of loop is used to iterate over the values of an iterable object....
🌐
W3Schools
w3schools.com › js › js_loop_for.asp
JavaScript for Loop
exp 3 increments the value of the initial variable (i++). ... exp 3 can do anything like negative increment (i--), positive increment (i = i + 15), or anything else. exp 3 can be omitted (if you increment the value inside the loop): const cars = ["BMW", "Volvo", "Saab", "Ford"]; let len = cars.length; let i = 0; let text = ""; for (; i < len; ) { text += cars[i] + "<br>"; i++; } Try it Yourself »
🌐
DEV Community
dev.to › swastikyadav › difference-between-forof-and-forin-loop-in-javascript-j2o
Difference between for...of and for...in loop in JavaScript. - DEV Community
October 22, 2023 - So, for...of loop does not work on objects. In simple words, for...of works with strings and arrays but not with objects.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › explain-the-differences-between-for-in-and-for-of-statement-in-javascript
Differences Between for-in and for-of Statement in JavaScript - GeeksforGeeks
July 12, 2025 - Conversely, the for..of loop is intended to iterate directly over values in iterable collections like arrays, strings, Maps, or Sets. The JavaScript for-in loops through the enumerable properties of an object.
Top answer
1 of 2
21

A bit of backnowledge to explain your question:

In Javascript, Objects (among these are Arrays) store properties in key-value pairs. That means that each assigned value has a key (the property name) to access it. For example in

person[name] = 'Tom'

person is the Object, name the key and 'Tom' the corresponding value.

Arrays use indices (i.e. numbers) as keys:

array[5] = 10

Now, keep that in mind in the following explanation.

Let's start with the traditional for loop:

for(let i = 0; i < array.length; i++) {...}

This way of iterating over an array is the oldest of the bunch (as long as you are not using while-loops) You'll find a corresponding way of writing a for loop in pretty much any (imperative) programming language. You'll notice, it is very explicit in the way it works. E.g. you could change the break-condition i < array.length to something else (e.g. i < array.length-1 for skipping the last position), or the step i++ (e.g. to i+=2), or start at a different index (e.g. let i = 5). You can iterate backwards instead of forwards through the array, if you want. Pretty much anything you can do in another for loop, you can do in this kind as well, if you know how.

Inside the brackets {...} you can then use i as a key to access the arrays values

Now this is all very powerful and nice, but it gets cumbersome to write every time, especially if in most of the cases you just want to iterate over an array. But luckily we have for-in:

For-in will retrieve you all the keys you have set yourself. With an array, you can use that to achieve the same result as above using

for(let i in array) {...}

Note however, that for-in is not only gonna give you the number keys from an array. It is also gonna work on other keys you have set yourself on any object:

let object = {
    key1 : 'value',
    key2 : 'value'
}

for(let i in object) {
    console.log(i)
}

will log out 'key1' and 'key2' (yes, the keys as well are strings here).

For a bit more precise description of what keys exactly it will give you, have a look at the link below.

When would you use for-in? Whenever you want to call some code for each element of an array / (almost) each property of an object once. E.g. when you want to increment all values in an array by 1. When not to use for-in? Don't use it if you need more granular control over the order you traverse the array in, or you don't want to hit all elements of the array, but only every second/third.

For an excellent resource on for-in loops I recommend Mozilla Developer Network

So, what are for-of loops then?

For-of loops are syntactically (i.e. the way you write them) very similar to for-in loops:

for(let v of array) {...}

However, for one, they are only going to work on so-called iterable objects (arrays are iterable). Second, you only get the values. They are no longer going to give you any keys!

let array = ['a', 'b', 'c']
for(let v of array) {
    console.log(v)
}

logs 'a', 'b' and 'c'. You won't know anymore, what keys these values had!

So, when to use these? Every time you just need the values and don't care about the keys. E.g. if you just want to print the values. When not to use them? If you want to swap elements of an array, if you want to reverse order. You can't even increment the values by 1 and store them back into the array, because for that, you would need to know their corresponding key.

For more information on for-in loops as well as what iterable actually means, again, I recommend the MDN

2 of 2
2

The only key differences that you'll have to consider would be

  • Say you want to start your loop from a specific index, you can do that with your traditional for loop but not using for...of.

  • You can't access indices of your array elements natively. You'll need to go for a workaround to achieve this.

      //Cannot access indices
      for(const num of [11,22,33,44,55]){
         console.log(num);
      }
    
    
      //Workaround
      for(const [index, num] of [11,22,33,44,55].entries()){
         console.log(num, index);
      }
    
🌐
Medium
medium.com › @bennythedev › the-difference-between-for-in-and-for-of-in-javascript-c50f40438af3
The Difference Between (for…in) and (for…of) in JavaScript | by Jamie Benjamin | Medium
July 13, 2023 - On the other hand, for...of is not designed to handle such complex structures and is primarily suited for straightforward iteration over the values of an iterable like arrays, strings, maps, or sets.
🌐
Medium
medium.com › @trig79 › javascript-the-early-lessons-for-vs-foreach-vs-for-of-loops-iteration-which-one-is-better-b557f385045
JavaScript The Early Lessons: For Vs forEach Vs For Of. Loops & Iteration, Which One Is Better? | by Trig | Medium
April 12, 2022 - Hands up! It was only recently that I started to use ‘For Of’ a lot more regularly. My go-to loop was forEach(), which I discuss towards the end of this article. For Of and its partner For In are great and spoiler alert, these loops are the ones you should use as your everyday go-to loops.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Difference between classic for loop and for...of loop in JS? - JavaScript - The freeCodeCamp Forum
September 17, 2020 - Hi all. 😀 I’ve learned how to use the classic for loop and the for…of loop. And I was wondering about the difference between classic for loop and the for…of loop. 😛 For example with a classic for loop: console.log("Counting from 0 to 9..."); // This prints "Counting from 0 to 9...". for (var i = 0; i
🌐
bitsofcode
bitsofco.de › for-in-vs-for-of
for..in versus for..of Loops | bitsofcode
November 15, 2016 - The for..in method provides us the most straightforward way to loop over Object keys and values, since Objects do not have access to the forEach method that Arrays do. The "key" for values in an Array are the numerical indexes. Therefore, these indexes are essentially just enumerable properties, like Object keys, except they are integers instead of strings.
🌐
W3Schools
w3schools.com › js › js_loop_forof.asp
JavaScript For Of
JS Examples JS HTML DOM JS HTML ... Prep JS Bootcamp JS Certificate JS Reference ... The JavaScript for of statement loops through the values of an iterable object....
🌐
Leanylabs
leanylabs.com › blog › js-foreach-map-reduce-vs-for-for_of
Performance of JavaScript .forEach, .map and .reduce vs for and for..of
While we partially agree, that got us thinking: “should programmers always prefer .map, .reduce, and .forEach Array methods over simple loops in JavaScript?” · Declarative programming style is very expressive, easier to write, and far more readable. It’s better 99% of the time, but not when performance matters.
🌐
Reddit
reddit.com › r/javascript › [askjs] difference between for loops
r/javascript on Reddit: [AskJS] Difference between For Loops
November 26, 2021 -

Hi Guys,

I've been wondering this for a while now. Is there a specific use case where one would use a regular for loop instead of a forEach loop and vice versa? To me, these loops work in exactly the same way, just written differently. Was wondering if anyone had any concrete examples as to where to use one and not the other

Any responses greatly appreciated!

Thanks

EDIT: Hey everyone! Thank you very much for your responses. It’s nice to know there is a place like this where people can come together to help others. I have seen no animosity here, which is a first on Reddit for me!

All of your comments have been really helpful and I hope to take this knowledge to help with my skills as a web dev and to become a better programmer as a whole!

Thanks again!

Top answer
1 of 5
151
Some differences include: for loops for (let i = 0; i < arr.length; i++) { // loop code } User defined loop conditions Allows breaking (and continuing) Supports await/yield for surrounding scope Standard usage for arrays gives you index only, not value Tends to be more complicated than other loops (less readable, more error prone) But also most flexible in how looping is defined for of loops for (const element of arr) { // loop code } Loops through iterables only Allows breaking (and continuing) Supports await/yield for surrounding scope Standard usage for arrays gives you value only, not index Other helpers needed for specific kinds of iteration, like array.entries() to get indices and values, or Object.keys() for going through keys of objects For arrays, loops through all values, even holes when arrays are sparse for in loops for (const key in obj) { // loop code } Loops through any value Iterates through value keys, both own and inherited (Object.keys(), by comparison, only provides own keys, not inherited) Allows breaking (and continuing) Supports await/yield for surrounding scope For arrays does not include holes when arrays are sparse (not recommended for looping through arrays) forEach loops arr.forEach(function (element, index, arr) { // loop code }) Only usable for collections that have this method (arrays, typed arrays, and some others like NodeLists from DOM API) Uses a callback function rather than looping through a code block Does not allow breaking Does not support await/yield for surrounding scope (though you can await and yield in the callback itself, it doesn't affect the looping) Gives you value, index, and collection being looped for each iteration For arrays does not include holes when arrays are sparse Often considered the most readable, especially when reusing callback functions (arr.forEach(doThing)) though can also be least performant
2 of 5
25
There are only really 2 relevant differences I feel like I come across often in daily use of the language. One is that you can't 'break' a forEach. forEach will iterate the entire iterable every time, whereas in an old fashioned for loop you can use the break keyword to stop execution of the ​loop whenever you like. The second is awaiting an async function within the iteration. forEach doesn't support awaiting within the function you provide it, where as an old fashioned for loop will handle it nice and smoothly. There are other subtle difference (like obviously you need to have an array to iterate over to use forEach), but as you say, if you are using a for loop for iteration then they are similar. There are also other iterative methods which can and will provide both of the things mentioned above, but for some reason when I need to use either of them I find the old fashioned for loop functional and charming
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. ... The initializing expression initialization, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity.
🌐
Programiz
programiz.com › javascript › for-of
JavaScript for... of Loop
The for..of loop in JavaScript allows you to iterate over iterable objects (arrays, sets, maps, strings etc).
🌐
DigitalOcean
digitalocean.com › community › tutorials › for-loops-for-of-loops-and-for-in-loops-in-javascript
JavaScript For Loops | DigitalOcean
August 26, 2021 - In this tutorial, we will learn about the for statement, including the for...of and for...in statements, which are essential elements of the JavaScript programming language. The for statement is a type of loop that will use up to three optional expressions to implement the repeated execution of a code block.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-for-of-loop
JavaScript for...of Loop - GeeksforGeeks
August 5, 2025 - It is better choice for traversing items of iterables compared to traditional for and for in loops, especially when we have break or continue statements.