How do you check if a variable is an array in JavaScript?

const list = [1, 2];
const obj = { a: 1 };

console.log(typeof list);
console.log(Array.isArray(list));
console.log(Array.isArray(obj));

Output:

Tutorials Class - Output Window

object
true
false

typeof [] is "object", so use Array.isArray(). See Data Types.


Learn more about the similar topics:
Tutorials
JavaScript Arrays
JavaScript Array Methods
JavaScript Objects
Exercises & Assignments
Write a JavaScript program to find the sum and average of array elements
Write a JavaScript program to find the largest and smallest number in an array
Write a JavaScript program to remove duplicates from an array
Write a JavaScript program to filter even numbers and double them
Write a JavaScript program to count the frequency of words in a sentence
Interview Questions & Answers
What is the difference between map and forEach?
What is the difference between slice and splice?
What is the difference between shallow copy and deep copy in JavaScript?
What is the difference between spread and rest operators?
What are Map and Set in JavaScript?
How do you check if a variable is an array in JavaScript?