DEV Community

Discussion on: Kyle Simpson proved I STILL don't know JavaScript (arrays)

Collapse
 
mattmcmahon profile image
Matt McMahon

Yep. Beat me to it. It's all the difference between having an ownProperty or not.

I'm wondering, since I've never tried to do this and can't conveniently try it right now, what the in operator reports for things like 0 in [,1,2]? Is that statement true or false? Or is it a syntax error?

Collapse
 
paceaux profile image
Paceaux

I tested five loops: for in, for of, forEach, map and for of was the only one that iterated on all slots.

Here's a gist of what I wrote. Feel free to run it in a few different browsers and tell me if my tests were wrong.

Collapse
 
paceaux profile image
Paceaux

I went ahead and updated my test gist with an if in test to see what it did.

const slotted = [,,'one'];
let i = 0;

while (i < slotted.length) {
 if (i++ in slotted) {
  console.log(`${i - 1} is in the array`);
 }
}

it, too, will inform us that only an index of 2 exists. This I think further confirms that these items in the array are not "hidden properties" at all.