Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 66971

What is the purpose of >>> bitwise shift operator in javascript?

I've seen in some code samples it being used in scenarios like this:

var len = t.length >>> 0;

What's confusing is that it didn't even need to be there, I'm not sure why this addition was added. MDN uses it a lot in there JavaScript prototype backwards compatibility additions as well.

var len = t.length; // has the same result

What purpose does the >>> serve (especially in this scenario) ?

MDN documentation

Zero-fill right shift a >>> b Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeros from the left.

Upvotes: 1

Views: 174

Answers (1)

Incognito
Incognito

Reputation: 20765

It appears to be used to typecast the length value to zero.

x = {};  //create object
x.length //is undefined
x.length >>> 0 //returns 0

Upvotes: 1

Related Questions