JavaScript Interview Question #38
How can you check whether a value is an array?
Objects, Arrays & Core Data Handling Junior Beginner
Quick Interview Answer
Use Array.isArray(value). It reliably determines whether a value is an Array, including cases where typeof would only report "object".
Detailed Explanation
Use Array.isArray(value). It reliably determines whether a value is an Array, including cases where typeof would only report "object".
Using typeof is insufficient because arrays, plain objects, and null all produce object-related results rather than identifying arrays specifically.
Code Example
const values = [1, 2, 3];
const user = { id: 1 };
console.log(Array.isArray(values)); // true
console.log(Array.isArray(user)); // false
console.log(typeof values); // "object"