# How to check if an object is an array in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published May 13, 2020, updated Jun 13, 2020. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-check-if-object-is-array

> A short guide to learn how to check if a variable is an array in vanilla JavaScript.

You may have used the `typeof` operator in JavaScript to check the type of an [object](https://attacomsian.com/blog/objects-prototypes-classes-javascript#objects). But, unfortunately, it doesn't work for [arrays](https://attacomsian.com/blog/javascript-arrays). The real issue is the way arrays are handled in JavaScript.

A JavaScript array is internally treated as a special kind of object. The square brackets syntax used for accessing an array element `array[index]` closely resembles what we use for an object `object[key]`. The only tangible difference is that index numbers are used as keys.

The simplest way to check if a variable is an array in JavaScript is by using the `Array.isArray()` method:

```javascript
Array.isArray(['?', '?', '?']); // true
Array.isArray([]); // true
Array.isArray({ name: 'John Deo', age: 21 }); // false
```

The `Array.isArary()` method works in all modern browsers, and IE9 and up. For older browsers support, just use the following [polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Polyfill):

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

Take a look at [this article](https://attacomsian.com/blog/javascript-arrays) to learn more about JavaScript arrays and how to use them to store multiple values in a single variable.
