What is the difference between for…in and for…of?

const arr = ["a", "b"];

for (const i in arr) console.log(i);
for (const v of arr) console.log(v);

const obj = { x: 1, y: 2 };
for (const key in obj) console.log(key);

Output:

Tutorials Class - Output Window

0
1
a
b
x
y

Use for...of for arrays, and for...in for the keys of an object. See JavaScript Loops.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.