Array.isArray()
ES5+Determines whether the passed value is an Array.
Syntax
Array.isArray(value)Parameters
value any The value to be checked
Return Value
true if the value is an Array; otherwise, false
Examples
console.log(Array.isArray([1, 2, 3]));
console.log(Array.isArray('hello'));
console.log(Array.isArray({length: 3})); 📌 When to Use
Use Array.isArray() whenever you need to check if a value is an array. It's the most reliable method for array detection, working correctly across different JavaScript contexts (iframes, different realms) where instanceof Array fails.
⚠️ Common Mistakes
Using typeof arr === "object" - arrays return "object", not "array"
Using instanceof Array - fails across different iframe/window contexts
Checking for .length property - many objects have length (strings, NodeList, arguments)
✅ Best Practices
Always use Array.isArray() over instanceof Array for reliable detection
Combine with type guards in TypeScript: if (Array.isArray(x)) { /* x is array */ }
Use for input validation in functions that accept arrays or single values
⚡ Performance Notes
Array.isArray() is O(1) and very fast. It's implemented natively and has negligible overhead. It's faster than checking constructor.name or using Object.prototype.toString.call(). Safe to use in hot paths.
🌍 Real World Example
Normalizing Function Input to Always Be an Array
Create a function that accepts either a single value or an array and handles both uniformly
function processItems(input) {
// Normalize input to always be an array
const items = Array.isArray(input) ? input : [input];
return items.map(item => transform(item));
}
// Works with both:
processItems('single'); // processes ['single']
processItems(['a', 'b', 'c']); // processes ['a', 'b', 'c']
// Type checking in validation
function validateData(data) {
if (!Array.isArray(data.tags)) {
throw new Error('tags must be an array');
}
// data.tags is guaranteed to be an array here
}