I was looking at code from Mozilla that add a filter method to Array and it had a line of code that confused me.
var len = this.length >>> 0;
I have never seen >>> used in JavaScript before.
What is it and what does it do?
It doesn’t just convert nonNumbers to Number, it converts them to Numbers that can be expressed as 32bit unsigned ints.
Although JavaScript’s Numbers are doubleprecision floats(*), the bitwise operators (<<
, >>
, &
, 
and ~
) are defined in terms of operations on 32bit integers. Doing a bitwise operation converts the number to a 32bit signed int, losing any fractions and higherplace bits than 32, before doing the calculation and then converting back to Number.
So doing a bitwise operation with no actual effect, like a rightwardshift of 0 bits >>0
, is a quick way to round a number and ensure it is in the 32bit int range. Additionally, the triple >>>
operator, after doing its unsigned operation, converts the results of its calculation to Number as an unsigned integer rather than the signed integer the others do, so it can be used to convert negatives to the 32bittwo’scomplement version as a large Number. Using >>>0
ensures you’ve got an integer between 0 and 0xFFFFFFFF.
In this case this is useful because ECMAScript defines Array indexes in terms of 32 bit unsigned ints. So if you’re trying to implement array.filter
in a way that exactly duplicates what the ECMAScript Fifth Edition standard says, you would cast the number to 32bit unsigned int like this.
(In reality there’s little practical need for this as hopefully people aren’t going to be setting array.length
to 0.5
, 1
, 1e21
or 'LEMONS'
. But this is JavaScript authors we’re talking about, so you never know…)
Summary:
1>>>0 === 1
1>>>0 === 0xFFFFFFFF 1>>0 === 1
1.7>>>0 === 1
0x100000002>>>0 === 2
1e21>>>0 === 0xDEA00000 1e21>>0 === 0x21600000
Infinity>>>0 === 0
NaN>>>0 === 0
null>>>0 === 0
'1'>>>0 === 1
'x'>>>0 === 0
Object>>>0 === 0
(*: well, they’re defined as behaving like floats. It wouldn’t surprise me if some JavaScript engine actually used ints when it could, for performance reasons. But that would be an implementation detail you wouldn’t get to take any advantage of.)
The unsigned right shift operator is used in the all the array extra’s method implementations of Mozilla, to ensure that the length
property is a unsigned 32bit integer.
The length
property of array objects is described in the specification as:
Every Array object has a length property whose value is always a nonnegative integer less than 2^{32}.
This operator is the shortest way to achieve it, internally array methods use the ToUint32
operation, but that method is not accessible and exist on the specification for implementation purposes.
The Mozilla array extras implementations try to be ECMAScript 5 compliant, look at the description of the Array.prototype.indexOf
method (§ 15.4.4.14):
1. Let O be the result of calling ToObject passing the this value as the argument. 2. Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length". 3. Let len be ToUint32(lenValue). ....
As you can see, they just want to reproduce the behavior of the ToUint32
method to comply with the ES5 spec on an ES3 implementation, and as I said before, the unsigned right shift operator is the easiest way.
Driis has sufficiently explained what the operator is and what it does. Here’s the meaning behind it/why it was used:
Shifting any direction by 0
does returns the original number and will cast null
to 0
. It seems that the example code that you are looking at is using this.length >>> 0
to ensure that len
is numeric even if this.length
is not defined.
For many people, bitwise operations are unclear (and Douglas Crockford/jslint suggests against using such things). It doesn’t mean that its wrong to do, but more favorable and familiar methods exist to make code more readable. A more clear way to ensure that len
is 0
is either of the following two methods.
// Cast this.length to a number
var len = +this.length;
or
// Cast this.length to a number, or use 0 if this.length is
// NaN/undefined (evaluates to false)
var len = +this.length  0;
That is the unsigned right bit shift operator. The difference between this and the signed right bit shift operator, is that the unsigned right bit shift operator (>>>) fills with zeroes from the left, and the signed right bit shift operator (>>) fills with the sign bit, thus preserving the sign of the numerical value when shifted.
>>>
is the unsigned right shift operator (see p. 76 of the JavaScript 1.5 specification), as opposed to the >>
, the signed right shift operator.
>>>
changes the results of shifting negative numbers because it does not preserve the sign bit when shifting. The consequences of this is can be understood by example, from an interpretter:
$ 1 >> 0
1
$ 0 >> 0
0
$ 1 >> 0
1
$ 1 >>> 0
1
$ 0 >>> 0
0
$ 1 >>> 0
4294967295
$(1 >>> 0).toString(16)
"ffffffff"
$ "cabbage" >>> 0
0
So what is probably intended to be done here is to get the length, or 0 if the length is undefined or not an integer, as per the "cabbage"
example above. I think in this case it is safe to assume that this.length
will never be < 0
. Nevertheless, I would argue that this example is a nasty hack, for two reasons:

The behavior of
<<<
when using negative numbers, a sideeffect probably not intended (or likely to occur) in the example above. 
The intention of the code is not obvious, as the existence of this question verifies.
Best practice is probably to use something more readable unless performance is absolutely critical:
isNaN(parseInt(foo)) ? 0 : parseInt(foo)
Two reasons:

The result of >>> is an “integral”

undefined >>> 0 = 0 (since JS will try and coerce the LFS to numeric context, this will work for “foo” >>> 0, etc. as well)
Remember that numbers in JS have an internalrepresentation of double.
It’s just a “quick” way of basic input sanity for length.
However, 1 >>> 0 (oops, likely not a desired length!)
Tags: java, javascript