Member-only story
Javascript — How to check if a variable is an Array?
1 min readJul 23, 2020
In JavaScript, we can check if a variable is an array by using 3 methods.
- isArray method
- instanceof operator
- checking the constructor type if it matches an Array object
Method 1: isArray Method
Checks whether the passed variable is an Array object.
Syntax:
Array.isArray(variableName)
Method 2: instanceof operator
Used to test whether the prototype property of a constructor appears anywhere in the prototype chain of an object.
Used to evaluate if the given variable has a prototype of ‘Array’.
Syntax:
variable instanceof Array
Method 3: Checking the constructor property of the variable
Checks its constructor with Array.
Syntax:
variable.constructor === Array