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
JavaScript Conditional Statements
JavaScript Loops
Exercises & Assignments
Write a JavaScript program to check even or odd number
Write a JavaScript program to find the largest of three numbers
Write a JavaScript program to check leap year
Write a JavaScript program to print the multiplication table of a number
Write a JavaScript program to find the factorial of a number
Write a JavaScript program to print the Fibonacci series
Write a JavaScript program to check prime number
Interview Questions & Answers
What is the difference between for…in and for…of?