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
1
  • Array.prototype instanceof Object = true Commented Feb 15, 2015 at 17:18

1 Answer 1

0

This is the correct way to implement isArray:

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

Read more here.

Sign up to request clarification or add additional context in comments.

2 Comments

The question is why is Array.prototype not an instanceof Array
thanks for your answer @Amir, but i really want understand why Array.prototype is not an instanceof Array, like haim said..