2

I wrote my own isArray function:

Array.isArray = function(value) {
  if (value instanceof Array) {
    console.log(true);
    return true;
  } else {
    console.log(false);
    return false;
  }
};

var isArray = Array.isArray;

isArray('String'); // false
isArray(202929); // false
isArray(true); // false
isArray(false); // false
isArray({}); // false
isArray(Array.prototype); // false, but must be true
isArray([]); // true

Why Array.prototype is not instance of Array since Array.prototype returns []?

> Array.prototype
[]
> Array.prototype instanceof Array
false
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474

1 Answers1

0

This is the correct way to implement isArray:

  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };

Read more here.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • 2
    The question is why is `Array.prototype` not an `instanceof Array` – haim770 Feb 15 '15 at 17:04
  • thanks for your answer @Amir, but i really want understand why Array.prototype is not an instanceof Array, like haim said.. –  Feb 15 '15 at 17:11