5

I am reading an implementation of Array.prototype.some on developer.mozilla.org

It contains this intruiging piece of code:

var t = Object(this);
var len = t.length >>> 0;

for (var i = 0; i < len; i++) {

Why is it calling len = t.length >>> 0 instead of len = t.length?

What difference does >>> 0 make?

Andrew Shepherd
  • 40,674
  • 26
  • 128
  • 192

1 Answers1

7

performs a logical (unsigned) right-shift of 0 bits, which is equivalent to a no-op. However, before the right shift, it must convert the x to an unsigned 32-bit integer. Therefore, the overall effect of x >>> 0 is convert x into a 32-bit unsigned integer.

Arun Prasath
  • 102
  • 6
  • 1
    +1. To directly address OP's question of why not use `t.length`: this is a nice way to ensure that `len` ends up as an integer, regardless of what value (if any) is stored in the `length` property of the object. – Ted Hopp May 29 '15 at 03:37