

Object.keys( object-name-goes-here) > Returns the keys or properties of an object. In javaScript, we can't loop through objects normally as we would on arrays, so, there are a few elements we can use to access either of our choices from an object.

Now Reason for the output being that we have Four(4) properties in our profile object and indexing as we all know starts from 0.n, so, we get the index of properties 0,1,2,3 since we are working with the for.in loop.įor.of loop* can return either the property, value or both, Let's take a look at how. Because for.in loops loop over all enumerable properties, this means if you add any additional properties to the array's prototype, then those properties will also appear in the loop. The for.in loop improves upon the weaknesses of the for loop by eliminating the counting logic and exit condition.Įxample: const digits = īut, you still have to deal with the issue of using an index to access the values of the array, and that stinks it almost makes it more confusing than before.Īlso, the for.in loop can get you into big trouble when you need to add an extra method to an array (or another object). let pets = new Set() Ĭonsole.log(pet) // "Cat", "Dog", "Hamster" Objects like Map and Set implement erator property allowingĪccess to stored values. Hand, is mainly interested in values of iterable objects. Here is an example that demonstrates this distinction: let list = Īnother distinction is that for.in operates on any object it servesĪs a way to inspect properties on this object. Of the numeric properties of the object being iterated. The object being iterated, whereas for.of returns a list of values Iterated on are different though, for.in returns a list of keys on I found a complete answer at Iterators and Generators (Although it is for TypeScript, this is the same for JavaScript too)īoth for.of and for.in statements iterate over lists the values
