Check if a Variable Is an Array in JavaScript
In JavaScript, not everything that looks like an array is truly an array. Sometimes, we deal with array-like structures such as strings or the arguments
object. So how do we confidently know if a variable is actually an array?
This tutorial walks you through the most reliable and beginner-friendly ways to check if a variable is a true JavaScript array. You'll learn how each method works, when to use it, and see real examples with common values like "apple"
, "banana"
, and more.
Method 1: Using Array.isArray()
This is the most recommended and reliable method to check if a variable is an array. Introduced in ECMAScript 5, Array.isArray()
returns true
if the given value is an array.
const fruits = ["apple", "banana", "cherry"];
const text = "apple, banana, cherry";
console.log(Array.isArray(fruits)); // true
console.log(Array.isArray(text)); // false
true
false
Method 2: Using instanceof Array
The instanceof
operator checks if a variable is an instance of a particular class. When used with arrays, it checks if the prototype chain contains Array
. This works well in most cases—but can fail across different execution contexts (like iframes).
const items = ["Item 1", "Item 2"];
const obj = { 0: "Item 1", 1: "Item 2" };
console.log(items instanceof Array); // true
console.log(obj instanceof Array); // false
true
false
Method 3: Using Object.prototype.toString.call()
This technique taps into JavaScript’s internal classification of types. It’s highly reliable—even across frames—and is often used in utility libraries like Lodash or jQuery. The output is a string like "[object Array]"
for real arrays.
const arr = ["apple"];
const str = "apple";
const num = 42;
console.log(Object.prototype.toString.call(arr)); // [object Array]
console.log(Object.prototype.toString.call(str)); // [object String]
console.log(Object.prototype.toString.call(num)); // [object Number]
[object Array]
[object String]
[object Number]
Bonus: Comparing with typeof
(Not Recommended)
It’s tempting to use typeof
to check if something is an array—but don’t. Arrays are technically objects, and typeof
can’t tell the difference between a plain object and an array. This method is included here to show why it’s not the right tool.
const list = ["apple"];
const dict = { fruit: "apple" };
console.log(typeof list); // object
console.log(typeof dict); // object
object
object
Which Method Should You Use?
✅ Use Array.isArray()
— it’s the modern, clean, and safest method for most projects.
✅ Use Object.prototype.toString.call()
if you're writing utility functions or frameworks where deep type checking is important.
⚠️ Use instanceof
only in simple cases, especially when cross-frame consistency isn’t a concern.
⛔ Avoid typeof
— it cannot differentiate arrays from objects.
Final Thoughts
Knowing whether a variable is truly an array can make or break your logic—especially when working with dynamic data, user input, or API responses. It’s not just about checking type; it’s about building predictable, bug-resistant programs.
Fortunately, JavaScript gives you more than one way to handle the task. Stick with Array.isArray()
in day-to-day projects, and explore Object.prototype.toString
when building utilities. Once you get used to these patterns, spotting arrays in your code becomes second nature.
Comments
Loading comments...