I was just looking through Mozilla Developer documentation, and found notation that I don't know what is used for and also cannot find any information through internet.
Array filter polyfill - line 10
var t = Object(this);
var len = t.length >>> 0;
Any suggestions what this operator is for ?
TL;DR
t.length >>> 0;
actually tries to get a valid 32 bit unsigned integer from t.length
. For what we know, t.length
could be of any type (an object, array, string etc). >>> 0
returns the value unaltered if it is already a valid 32 bit unsigned number. For example,
console.log({} >>> 0);
// 0
console.log([] >>> 0);
// 0
console.log("Google" >>> 0);
// 0
Normally, this bitwise trick is used to avoid a if
block type checking, like this
var len = 0;
if (typeof data === 'number') {
len = data;
}
We might still have to convert len
to an integer, if it is a floating point value.
Explanation
>>>
is called Zero-fill right shift operator. Apart from being used as a bitwise operator, it is used to get a 32 bit numeric value out of an object. The ECMA Script 5.1 Specification for >>>
says that
- Let lref be the result of evaluating ShiftExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating AdditiveExpression.
- Let rval be GetValue(rref).
- Let lnum be ToUint32(lval).
- Let rnum be ToUint32(rval).
- Let shiftCount be the result of masking out all but the least significant 5 bits of rnum, that is, compute rnum & 0x1F.
- Return the result of performing a zero-filling right shift of lnum by shiftCount bits. Vacated bits are filled with zero. The result is an unsigned 32-bit integer.
It basically converts both the operands to 32 bit unsigned integer (Step 5 and 6) and shifts the left hand side expression, right hand side expression times.
If we look at the definition of ToInt32
,
- Let number be the result of calling ToNumber on the input argument.
- If number is NaN, +0, −0, +∞, or −∞, return +0.
- Let posInt be sign(number) * floor(abs(number)).
- Let int32bit be posInt modulo 232; that is, a finite integer value k of Number type with positive sign and less than 232 in magnitude such that the mathematical difference of posInt and k is mathematically an integer multiple of 232.
- Return int32bit.
First the argument is converted to a number (if it is a not a valid number then NaN
will be returned by ToNumber
). And step 4 makes sure that you return a valid number in the range 0 to 232.
It's a "Zero-fill right shift" (bitwise) operator.
This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left. The sign bit becomes
0
, so the result is always non-negative.For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. For example,
9 >>> 2
yields2
, the same as9 >> 2
:However, this is not the case for negative numbers. For example,
-9 >>> 2
yields1073741821
, which is different than-9 >> 2
(which yields-3
):
@thefourtheye answered with a good explanation about the usage of this operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With