for in / for of loops in javascript | when to use for in loop ? | when to use for of loop

Опубликовано: 07 Октябрь 2024
на канале: Quick Code Academy
62
5

for..in is a method for iterating over "enumerable" properties of an object. It therefore applies to all objects (not only Object()s) that have these properties.

An enumerable property is defined as a property of an object that has an Enumerable value of true. Essentially, a property is "enumerable", if it is enumerable. We can check if a property is enumerable by calling property.enumerable, which will return true or false.

We use the for..in loop with the following syntax -

for (variable in enumerable) {
// do stuff
}


The for..of syntax is essentially a wrapper around the [Symbol.iterator] to create loops. It uses the following syntax -

for (variable of iterable) {
// do stuff
}