tags:

views:

114

answers:

4

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?

+1  A: 

It is an unsigned right shift operator in my belief

jsshah
Aye: See: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Bitwise_Operators
Wevah
+1  A: 

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
+4  A: 

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

olliej
+1 - For explaining why they are using it here.
James Black
A: 

See also http://stackoverflow.com/questions/1385491/javascript-why-use-around-arguments-and-why-use-when-extracting-the:

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

andre-r