Different Between for...in and for...of loops in Javascript

Posted on 5/8/2025

The for...of and for...in in Javascript have different behaviors. Here I will show the difference when looping through an array:

const datas = [1, 3, 5, 8, 9, 10]

for (const data of datas) {
    console.log(data) // Output: 1, 3, 5, 8, 9, 10
}

for (const data in datas) {
    console.log(data) // Output: 0, 1, 2, 3, 4, 5
}

From the example above, we can conclude that when using for...of, we will loop through the array values. Whereas when using for...in, we will loop through the array indexes.

Created with ❤️ using Next.js & Tailwind CSS