Languages
[Edit]
EN

JavaScript - check if variable is array type?

11 points
Created by:
Root-ssh
6586

In JavaScript it is possible to check is object array type in following way.

1. Built in Array.isArray method example

// ONLINE-RUNNER:browser;

console.log(Array.isArray([ ])); // true
console.log(Array.isArray(new Array())); // true

console.log(Array.isArray({ })); // false
console.log(Array.isArray(new Object())); // false
console.log(Array.isArray(5)); // false
console.log(Array.isArray('a')); // false
console.log(Array.isArray(true)); // false
console.log(Array.isArray(function() { })); // false
console.log(Array.isArray(new function() { })); // false

2. instanceof operator test example

Note: if web browser does not support Array.isArray method this approach is useful.

// ONLINE-RUNNER:browser;

if (!Array.isArray) {
    Array.isArray = function(array) {
        return (array instanceof Array);
    };
}

// Example:

console.log(Array.isArray([ ])); // true
console.log(Array.isArray(new Array())); // true

console.log(Array.isArray({ })); // false
console.log(Array.isArray(new Object())); // false
console.log(Array.isArray(5)); // false
console.log(Array.isArray('a')); // false
console.log(Array.isArray(true)); // false
console.log(Array.isArray(function() { })); // false
console.log(Array.isArray(new function() { })); // false

3. constructor property test example

Note: if web browser does not support Array.isArray method this approach is useful.

// ONLINE-RUNNER:browser;

if (!Array.isArray) {
    Array.isArray = function(array) {
        return array.constructor == Array;
    };
}

// Example:

console.log(Array.isArray([ ])); // true
console.log(Array.isArray(new Array())); // true

console.log(Array.isArray({ })); // false
console.log(Array.isArray(new Object())); // false
console.log(Array.isArray(5)); // false
console.log(Array.isArray('a')); // false
console.log(Array.isArray(true)); // false
console.log(Array.isArray(function() { })); // false
console.log(Array.isArray(new function() { })); // false

4. toString result test example (Polyfill extension)

Note: if web browser does not support Array.isArray method this approach is useful.

// ONLINE-RUNNER:browser;

// Polyfill:

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

// Example:

console.log(Array.isArray([ ])); // true
console.log(Array.isArray(new Array())); // true

console.log(Array.isArray({ })); // false
console.log(Array.isArray(new Object())); // false
console.log(Array.isArray(5)); // false
console.log(Array.isArray('a')); // false
console.log(Array.isArray(true)); // false
console.log(Array.isArray(function() { })); // false
console.log(Array.isArray(new function() { })); // false
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.