In the MDN Array PolyFils, it has the following line.
var len = t.length >>> 0;
Does binary shift right 0 really actualy do anything, or can I take this out?
In the MDN Array PolyFils, it has the following line.
var len = t.length >>> 0;
Does binary shift right 0 really actualy do anything, or can I take this out?
Of course it does something. Whatever the value may be, it'll convert it to an unsigned 32-bit number.
If a numeric conversion wasn't possible, you'll get 0
. This handles odd cases where .length
is some unexpected value.
This is basically how they implement instructions in the spec. Like in the specification for .forEach()
:
...
Let
lenValue
be the result of calling the[[Get]]
internal method ofO
with the argument"length"
.Let
len
beToUint32(lenValue)
.
>>>
is the unsigned right shift operator. The unsigned right shift operator is used in array method, to ensure that the length property is a unsigned 32-bit integer.
According to the specs:
Every Array object has a length property whose value is always a nonnegative integer less than 232.