Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
How can I check if a variable is an array in JavaScript? Pending Review
Asked on May 01, 2026
Answer
To check if a variable is an array in JavaScript, you can use the Array.isArray() method, which is a reliable and straightforward way to determine if a variable is an array.
<!-- BEGIN COPY / PASTE -->
const myVar = [1, 2, 3];
const isArray = Array.isArray(myVar);
console.log(isArray); // true
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- The Array.isArray() method returns true if the variable is an array, and false otherwise.
- This method is preferred over using typeof or instanceof because it accurately identifies arrays, even when dealing with different execution contexts (like iframes).
- Always ensure that the variable is defined before checking if it is an array to avoid potential errors.
Recommended Links:
