How to Check If Variable is Array in JavaScript

To check the data type of any value in JavaScript, use the typeof operator. There are some use cases like, let’s say, we are fetching the json data from the server, and then we convert it into an object, and then if we wanted to make sure that the data object contains the array values or not, use the isArray() function.

Check If Variable is Array in JavaScript

To check if a variable is an array in JavaScript, use the array.isArray() method.The array isArray() is a built-in function that checks whether the passed value is an array or not.

If the value is an array, true is returned; otherwise, false is returned. When checking for the Array instance, the isArray() function is preferred over the instanceof because it works through iframes.




The value is to be checked.


See the following example.

// app.js

let arr = ['Millie', 'Bobby', 'Brown'];

let data = {
  realname: 'Millie Bobby Brown',
  character: 'Eleven',
  series: 'Stranger Things'

In the above example, we have defined an array of Strings and an array of Objects.

See the following output.

➜  es git:(master) ✗ node app
➜  es git:(master) ✗

Javascript isArray behavior with different types

Empty Array [] true
An array of Number literal [1,2,3] true
An array of String literal [“a” “b”, “c”] true
Array (using constructor.from) [null,null,null,null,null,null,null,null,null,null] true
Array with empty values using constructor [null,null,null,null,null,null,null,null,null,null] true
Array with values using constructor [1,2,3] true
Array prototype [] true
Array from another window [1,2,3] true
Empty Object {} false
Object with .length property {“length”:10} false
Object whose __prototype__ is Array {} false
Object {“a”: “2”, “b”: “c”} false
Object with nested Array {“nested”:[“2”, “c”]} false
Empty Set {} false
Set of String {} false
Set of Number {} false
Empty Map {} false
Map with different types {} false
String literal “hello world.” false
String Object “hello world” false
Boolean true literal true false
Boolean false literal false false
Explicitly undefined undefined false
Implicit undefined undefined false
null null false
Number (integer) 123 false
Number (float) 1.23 false

An array can be checked if it is empty by using the array.length property. This property returns several elements in the array.

If the number is greater than 0, it evaluates to true.

This method and property can be used together with the AND(&&) operator to determine whether the array exists and is not empty.

Array isArray() Polyfill

Running the following code before any other code will create Array.isArray() if it’s not natively available in your browser.

if (!Array.isArray) {
  Array.isArray = function(arg) {
    return === '[object Array]';

That’s it for this tutorial.

See also

Javascript array to json

Javascript array toString

Javascript array reverse()

1 thought on “How to Check If Variable is Array in JavaScript”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.