What does Array.isArray() solve that typeof can't?
Short answer
typeof arr returns "object" for arrays, the same as for plain objects, so it can't distinguish them. Array.isArray() correctly identifies real arrays, even across different execution contexts like iframes.
Advertisement
AModel answer
typeof arr returns "object" for arrays, the same as for plain objects, so it can't distinguish them. Array.isArray() correctly identifies real arrays, even across different execution contexts like iframes.
Array.isArray([1,2,3]); // true
typeof [1,2,3]; // "object"Real-world example
Validating that an API response is actually an array before calling .map() on it.
Likely follow-ups
- How do you check if something is "array-like" but not a true array?
Related topics