What does this line do? arr.length >>> 0

What is it for:

arr.length >>> 0

And why should I use it?

+5
source share
2 answers

This is an unsigned right shift operator. In this case (when using c 0), it guarantees that it arr.lengthis an integer or, rather, is evaluated as a arr.length32-bit unsigned string integer value. (This means that it never NaN, never is negative and never has a decimal part.)

Examples:

'1'       >>> 0: 1
1         >>> 0: 1
''        >>> 0: 0
undefined >>> 0: 0
null      >>> 0: 0
1.0∙∙∙01  >>> 0: 1

Compare with:

Number('1')      : 1
Number(1)        : 1
Number('')       : 0
Number(undefined): NaN
Number(null)     : 0
Number(1.0∙∙∙01) : 1.0∙∙∙01

It is there to provide the correct length.

+8
source

Provides what .lengthis a 32-bit integer.

Array 32- ( , Array.prototype .length).

+5

All Articles