1

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?

David Bradshaw
  • 11,859
  • 3
  • 41
  • 70

2 Answers2

2

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():

  1. ...

  2. Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length".

  3. Let len be ToUint32(lenValue).

Community
  • 1
  • 1
cookie monster
  • 10,671
  • 4
  • 31
  • 45
1

>>> 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.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331