Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
So it makes sense that the spread syntax would copy that same array.
According to the spec, forEach elides missing array items, so all of those undefined elements won't be accounted for. Why it doesn't in the final case I don't know. Will have to come back and look into a bit.
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
So the difference is holes in an array versus undefined elements. And that happens due to this:
John Hardy
@jhlagado
The difference is that the spread operation creates an iterator which maps over all the elements. slice() operates at a lower level. twitter.com/laurieontech/s…
21:53 PM - 24 Jul 2019
Laurie
@laurieontech
Hey, @bitandbang, any idea what the underlying reason for this difference is? I've been scouring the docs and can't seem to find anything that implies the two results would be any different.
https://t.co/aCRfd5jblA
In [...arr], what the spread operator conceptually does is to get an iterator from arr (like for (const item of arr) { ... }), and the iterator returns undefined when it encounters a hole, rather than skipping it
Oh interesting. I just played around with this a bit. And found this.
So it makes sense that the spread syntax would copy that same array.
According to the spec, forEach elides missing array items, so all of those undefined elements won't be accounted for. Why it doesn't in the final case I don't know. Will have to come back and look into a bit.
Thanks for the example!
I just didn't know that
arr.slice()
and[...arr]
are not equivalent.Here's another example, without
forEach
.arr.slice()
vs[...arr]
So this is what I see
But no docs are telling me why that's the case. Still searching because I genuinely want to know!
So the difference is holes in an array versus undefined elements. And that happens due to this:
Also explained this way:
Awesome! Thank you!