JavaScript: for-in and for-of loops
1 min read

JavaScript: for-in and for-of loops

In JavaScript, the array is just a glorified object, and as such, using for-in loop iterates over the indices and gives unexpected results.

let letters = ['h', 'e', 'l', 'l', 'o'];
for (const l in letters){
	console.log(l);
} // prints 0 1 2 3 4

for-of loop gives the expected result.

let letters = ['h', 'e', 'l', 'l', 'o'];
for (const l of letters){
	console.log(l);
} // prints h e l l o