array like objects

Mike Samuel mikesamuel at
Mon Dec 7 18:46:15 PST 2009

2009/12/7 Breton Slivka <zen at>:
> The one that I use is
> function isArrayLike(i){
>    return (typeof i !=="string") && i.length !== void (0);

I like that it's efficient.
How about
    if (i !== null && typeof i === 'object') {
      var len = i.length;
      var ilen = len >>> 0;
      return (len === ilen && ilen >= 0 && ilen <= 0x80000000)
    return false;

Less efficient, but doesn't fail for null, skips functions which do
have a length, and requires length to be a valid array length only
performing one get and one toNumber.

> }

> It might not be perfect, but it allows me to make certain assumptions
> about the input that are useful enough. Keep in mind that an Array may
> have a length of 5, and all those values are undefined, so  {length:4}
> could be used as a valid arrayLike, and that seems reasonable to me.

More information about the es-discuss mailing list