How to check if an object is an array in JavaScript

#javascript #array

Last updated on Nov 16, 2020 by Suraj Sharma

In this tutorial, you will learn how you can check whether a JavaScript object is an array or not.

Suppose you have an array A

const A = [1,2,3,4]

typeof A always returns "object"

console.log(typeof A) // logs 'object'

therefore typeof is not a solution to determine if an object is an Array.

However, JavaScript Array provides a method isArray(), which checks whether the argument object passed to it, is an Array or not

Array.isArray({a: 1}) // false

Array.isArray([1,2,3,4]) // true

Array.isArray(null) // false

Array.isArray() is widely supported by all the browsers.

Related Solutions

Suraj Sharma is a JavaScript Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.