Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's array.length >>> 0; used for? [duplicate]

Tags:

javascript

Possible Duplicates:
1. What good does zero-fill bit-shifting by 0 do? (a >>> 0)
2. JavaScript triple greater than

I was digging through some MooTools code, and noticed this snippet being used in every array method:

var length = this.length >>> 0;

What's the benefit of doing this? It doesn't seem to me like it's some max length thing, as 3247823748372 >>> 0 === 828472596 and 3247823748373 >>> 0 === 828472597 (so, both are 1 higher, if it were for maxLength wouldn't it be better to do something like var length = Math.min(this.length, MAX_LENGTH)?).

like image 430
goto-bus-stop Avatar asked Nov 27 '11 16:11

goto-bus-stop


1 Answers

This seems to be the safest way to ensure length is a non-negative (32-bit) integer.

Just another example of where JavaScript lacks proper standard functions, this time for save conversion of unknown types into, well, 32-bit unsigned integers.

like image 185
Has QUIT--Anony-Mousse Avatar answered Oct 11 '22 14:10

Has QUIT--Anony-Mousse