DEV Community

loading...

Discussion on: Understanding Javascript Array Series IV - Array Loops & Iteration Part I

Collapse
camicode profile image
CamCode

Nice post!
thanks to you, I started to understand a little about arrays,but there is a point that I don't understand in the last example of your code when you want to add a property to the array using the type method: because as a final result it prints 0 , 1 , 2 ,nedy and not mixed? And in other for loop why don't print mixed? Thanks and continue with your posts!!!

Collapse
nedyudombat profile image
Nedy Udombat Author

Thanks @camicode . Please can you restructure your question, I do not quite understand it yet.

Collapse
camicode profile image
CamCode

Of course !! sorry but English it's not my first language, however I don't understand the final example. Can you explain me better why in both loops doesn't print "mixed"?
I hope it's clearer now :)

Thread Thread
nedyudombat profile image
Nedy Udombat Author

Okay great, in the first solution I used a for ... in loop with loops through the property name of an array. That array has the following property names 0, 1, 2, type.

The second solution I use for ... of which loops through the property values of an array, basically the elements of the array. 1, 2, "nedy";

If I want to print mixed which a property of the items array, I would have to do this console.log(items.type) // "mixed".

so for ... in deals with the property names, for ... of deals with the property values(elements of the array).

Thread Thread
camicode profile image
CamCode

Thanks for this explanation, now I understand better!

Thread Thread
nedyudombat profile image
Nedy Udombat Author

You are welcome @camicode