In JavaScript, we can check if a variable is an array by using 3 methods, using the isArray method, using the instanceof operator and using checking the constructor type if it matches an Array object.
- Method 1: Using the isArray method
The Array.isArray() method checks whether the passed variable is an Array object.
Syntax:
Array.isArray(variableName)
It returns a true boolean value if the variable is an array and false if it is not. This is shown in the example below.
Example-1:
<!DOCTYPE html><htmllang="en"><head><title>How to check if a variableis an array in JavaScript?</title></head><body><h1style="color: green">GeeksforGeeks</h1><b>How to check if a variableis an array in JavaScript?</b><p>Click on the button to checkif the variable is an array</p><p>Output for string:<divclass="outputString"></div><p>Output for number:<divclass="outputNumber"></div><p>Output for array:<divclass="outputArray"></div><buttononclick="checkArray()">Click here</button><scripttype="text/javascript">function checkArray() {let str = 'This is a string';let num = 25;let arr = [10, 20, 30, 40];ans = Array.isArray(str);document.querySelector('.outputString').textContent = ans;ans = Array.isArray(num);document.querySelector('.outputNumber').textContent = ans;ans = Array.isArray(arr);document.querySelector('.outputArray').textContent = ans;}</script></body></html>chevron_rightfilter_noneOutput:
- Method 2: Using the instanceof operator
The instanceof operator is used to test whether the prototype property of a constructor appears anywhere in the prototype chain of an object. This can be used to evaluate if an the given variable has a prototype of ‘Array’.Syntax:
variable instanceof Array
The operator returns a true boolean value if the variable is same as what is specified (here an Array) and false if it is not. This is shown in the example below.
Example-2:
<!DOCTYPE html><htmllang="en"><head><title>How to check if a variable isan array in JavaScript?</title></head><body><h1style="color: green">GeeksforGeeks</h1><b>How to check if a variable isan array in JavaScript?</b><p>Click on the button to checkif the variable is an array</p><p>Output for string:<divclass="outputString"></div><p>Output for number:<divclass="outputNumber"></div><p>Output for array:<divclass="outputArray"></div><buttononclick="checkArray()">Click here</button><scripttype="text/javascript">function checkArray() {let str = 'This is a string';let num = 25;let arr = [10, 20, 30, 40];ans = str instanceof Array;document.querySelector('.outputString').textContent =ans;ans = num instanceof Array;document.querySelector('.outputNumber').textContent =ans;ans = arr instanceof Array;document.querySelector('.outputArray').textContent =ans;}</script></body></html>chevron_rightfilter_noneOutput:

- Method 3: Checking the constructor property of the variable
Another method to check a variable is an array is by checking it’s constructor with Array.
Syntax:
variable.constructor === Array
This becomes true if the variable is same as what is specified (here an Array) and false if it is not. This is shown in the example below.
Example-3:
<!DOCTYPE html><htmllang="en"><head><title>How to check if a variable isan array in JavaScript?</title></head><body><h1style="color: green">GeeksforGeeks</h1><b>How to check if a variable isan array in JavaScript?</b><p>Click on the button to checkif the variable is an array</p><p>Output for string:<divclass="outputString"></div><p>Output for number:<divclass="outputNumber"></div><p>Output for array:<divclass="outputArray"></div><buttononclick="checkArray()">Click here</button><scripttype="text/javascript">function checkArray() {let str = 'This is a string';let num = 25;let arr = [10, 20, 30, 40];ans = str.constructor === Array;document.querySelector('.outputString').textContent = ans;ans = num.constructor === Array;document.querySelector('.outputNumber').textContent = ans;ans = arr.constructor === Array;document.querySelector('.outputArray').textContent = ans;}</script></body></html>chevron_rightfilter_noneOutput:

Recommended Posts:
- variable === undefined vs. typeof variable === âundefinedâ in JavaScript
- JavaScript | Check the existence of variable
- JavaScript | Check if a variable is a string
- How to check a variable is of function type using JavaScript ?
- Understanding variable scopes in JavaScript
- JavaScript | Set object key by variable
- How to change the value of a global variable inside of a function using JavaScript ?
- How to use a variable for a key in a JavaScript object literal?
- How to use dynamic variable names in JavaScript ?
- How to load the contents of a text file into a JavaScript variable?
- How to insert a JavaScript variable inside href attribute?
- JavaScript ReferenceError - Can't access lexical declaration`variable' before initialization
- JavaScript SyntaxError "variable" is a reserved identifier
- JavaScript ReferenceError - Assignment to undeclared variable
- JavaScript SyntaxError - Missing variable name
- JavaScript ReferenceError - variable is not defined
- How to add a property to a JavaScript object using a variable as the name?
- JavaScript TypeError - Variable "x" redeclares argument
- PHP | Check if a variable is a function
- How to check whether a variable is set or not using PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


