0

I just saw this code:

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        var len = this.length >>> 0; // 3rd line
        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0) from += len;

        for (; from < len; from++) {
            if (from in this && this[from] === elt) return from;
        }
        return -1;
    };
}

What does the >>> do on the 3rd line?

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 1
    possible duplicate of [What is the JavaScript >>> operator used for?](http://stackoverflow.com/questions/5716641/what-is-the-javascript-operator-used-for) and http://stackoverflow.com/questions/5747123/strange-javascript-operator-expr-0 – j08691 Sep 24 '13 at 14:02
  • 1
    you know how difficult it is googling for this? Yeah, I guess you do. – Michael Paulukonis Sep 24 '13 at 14:05
  • 2
    [Reading the manual](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators) will tell you what it does... – Elias Van Ootegem Sep 24 '13 at 14:07
  • Two upvotes... huh... if the same question has been posted by some new comer... series of downvoting would have triggered with some rude comments. – Thalaivar Sep 24 '13 at 14:10
  • I cannot really figure out how it's possible for someone with about 60k reputation to ask something which has already been asked. In all your years in SO, haven't you ever seen "check for duplicates" or "search before you ask" written anywhere? – Niccolò Campolungo Sep 24 '13 at 14:13
  • @LightStyle hmmm somehow I did not find the duplicate. Odd. – Naftali Sep 24 '13 at 15:19

3 Answers3

6

That's an unsigned right shift operator. Interestingly, it is the only bitwise operator that is unsigned in javascript.

Lets have a practical application of it.. suppose you want to divide a number by 4 yes 8/4 = 2 right !

what if you can do that using bitwise operations: that will be much faster right ?

do this in your console now..

20 >>> 2

gives 5

how ??

when we convert 20 to binary we get 10100

now shift 2 bits to right , you will get 101 which is equivalent to 5

Cheers!

Community
  • 1
  • 1
aelor
  • 10,892
  • 3
  • 32
  • 48
  • From what I can tell, this is a useful operator to use instead of `>>` if you want your calculations to give totally incorrect results when supplied with negative inputs. Otherwise it might be better to use `>>`. – AKHolland Sep 24 '13 at 14:54
2

From MDN docs on JavaScript operators:

Zero-fill right shift

a >>> b

Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators

It's a bitwise operator.

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