

Difference between for...of and for...in loop in JavaScript.
source link: https://dev.to/swastikyadav/difference-between-forof-and-forin-loop-in-javascript-j2o
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hello Everyone,
The difference between for-of and for-in loop really troubled me when I was learning JavaScript. And with this blog I will try to clear the confusion once and for all.
Let's understand them one by one.
for...of Loop
The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables.
I know that's not the explanation you came here for, So let me explain.
for...of loop works only with iterable objects. In JavaScript, iterables are objects which can be looped over.
String, Array, TypedArray, Map, and Set are all built-in iterables, because each of their prototype objects implements an @@iterator method. So, for...of loop works on the mentioned object types.
Object in JavaScript is not iterable by default. So, for...of loop does not work on objects.
- In simple words, for...of works with strings and arrays but not with objects.
For instance:
cosnt str = "Hello World";
for(element of str) {
console.log(element);
}
// H e l l o " " W o r l d
for...in Loop
The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.
Explanation:
So, for...of does not work with objects (non iterables), Then how do we loop over keys and values of an object? And the answer is for...in loop.
for...in works with those properties whose enumerable flag is set to true.
- Enumerable flag for properties created via simple assignment or property initializer are by default true.
- Enumerable flag for properties created via Object.defineProperty are by default false.
For instance:
const student = {
registration: "123456",
name: "Sandeep",
age: 33,
}
for(key in student) {
console.log(key, student[key]);
}
/*
registration "123465"
name "Sandeep"
age 33
*/
Now let's add a new property (marks) to student object and set it's enumerable flag to false. With enumerable flag set to false, marks key won't appear in result of for...in loop.
const student = {
registration: "123456",
name: "Sandeep",
age: 33,
}
Objec.defineProperty(student, "marks", {
value: 98,
enumerable: false,
})
console.log(student.marks);
// 98
for(key in student) {
console.log(key, student[key]);
}
/*
registration "123465"
name "Sandeep"
age 33
*/
// marks key does not show up in the for...in loop result.
for...in also works with strings and arrays, because enumerable flag for string and array properties are also by default true.
- In simple words use for...in to loop over objects. Although for...in works with strings and arrays, but it is not suggested to use that way.
Conclusion
- for...of - Use to loop over strings and arrays.
- for...in - Use to loop over objects.
That's it for this post.
I am starting a Newsletter where I will share epic weekly content to build your skillset. If you are interested please subscribe to 8020 NewsLetter.
Thank You!
Recommend
-
35
There is plenty of content out there describing what Continuous Integration, Continuous Delivery and Continuous Deployment are. But what are these processes for in the first place? It is crucial to understand the p...
-
14
JavaScript: The difference between match() and matchAll() String.prototype.match() String.prototype.match() is a meth...
-
10
What's the difference between these two method of defining a 'class' in JavaScript? Method One Define method within the constructor: function MyClass() { this.foo = function() { console.log('hello world!');...
-
13
In JavaScript, you can pass by value and by reference. The main difference between the two is that passing by value happens when assigning primitives while passing by reference when assigning objects. Let’s discuss values and...
-
10
The Difference Between a Class and a Prototype in JavaScriptJuly 14th 2021 new story8
-
7
Difference between SLICE & SPLICE in JavaScript Hello Devs, In this article, we will discuss what's the difference between the two most important methods of Array in JavaScript (i.e Slice and Splice) The...
-
5
Whenever I encounter null or undefined, I get incredibly confused by how each data type is used in JavaScript. I mean, what's the difference? Don't they both express the concept of nothing?
-
8
Every JS developer must have heard of the term Event Loop. Both JS and Node Js is based on the principle of event loop which has similarities and dissimilarities to some extent. Let's discuss the event loop in brief and find...
-
8
The difference between “undefined” and “not defined” in JavaScriptHow two words can decide if your code breaks or not
-
5
Muhammad Saim Hashmi ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK