Languages
[Edit]
EN

JavaScript - check if variable is array type

11 points
Created by:
Root-ssh
177650

In this article, we would like to show you how to check if an object or variable is array type using JavaScript.

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

Alternative titles

  1. JavaScript - check if object is array type
  2. JavaScript - detect if passed argument is array
  3. JavaScript - detect if passed parameter is array
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - Arrays (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join