1

This is Mozilla's Code in Mozilla's Array.prototype.indexOf

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt)
  {
    var len = this.length >>> 0; // What does ">>>" do?

    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;
  };
}

I don't understand some of the syntax.
What does ">>>" do in the above code?

Billy
  • 14,356
  • 28
  • 67
  • 99
  • 1
    Exact dup: http://stackoverflow.com/questions/1385491/javascript-why-use-around-arguments-and-why-use-when-extracting-the – Crescent Fresh Sep 25 '09 at 02:09

4 Answers4

6

It's an unsigned right shift -- they're basically do that as a very fast way to convert to a valid array index.

olliej
  • 32,789
  • 8
  • 55
  • 54
1

It is an unsigned right shift operator in my belief

jsshah
  • 1,701
  • 1
  • 10
  • 18
  • Aye: See: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Bitwise_Operators – Wevah Sep 25 '09 at 00:27
1

It is an unsigned right shift, as pointed out here: http://www.c-point.com/javascript_tutorial/jsoprurshift.htm, but it should be shifting by the number of bits in the second number (to the right of >>>).

James Black
  • 40,548
  • 9
  • 79
  • 153
0

See also Why use /*, */ around arguments and why use >>> when extracting the length of an array?:

"The >>> is an unsigned right shift. It's being used here to convert a potentially signed number length into an unsigned number."

Community
  • 1
  • 1
andre-r
  • 2,575
  • 17
  • 22