How to check if a variable is an array in JavaScript? - GeeksforGeeks

20 Jul, 2021
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 methodThe 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><html lang="en">  <head>    <title>      How to check if a variable      is an array in JavaScript?  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>      How to check if a variable       is an array in JavaScript?  </b>    <p>      Click on the button to check      if the variable is an array  </p>    <p>Output for string:        <div class="outputString">  </div>      <p>Output for number:        <div class="outputNumber">  </div>      <p>Output for array:        <div class="outputArray">  </div>        <button onclick="checkArray()">      Click here  </button>    <script type="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>Output:
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><html lang="en">  <head>    <title>      How to check if a variable      is an array in JavaScript?  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>      How to check if a variable       is an array in JavaScript?  </b>    <p>      Click on the button to check      if the variable is an array  </p>    <p>Output for string:        <div class="outputString">  </div>      <p>Output for number:        <div class="outputNumber">  </div>      <p>Output for array:        <div class="outputArray">  </div>        <button onclick="checkArray()">      Click here  </button>    <script type="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>
Output:
Method 2: Using the instanceof operatorThe 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 ArrayThe 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><html lang="en">  <head>    <title>      How to check if a variable is      an array in JavaScript?  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>      How to check if a variable is      an array in JavaScript?  </b>    <p>      Click on the button to check       if the variable is an array  </p>    <p>Output for string:        <div class="outputString"></div>      <p>Output for number:        <div class="outputNumber"></div>      <p>Output for array:        <div class="outputArray"></div>        <button onclick="checkArray()">Click here</button>    <script type="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>Output:
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><html lang="en">  <head>    <title>      How to check if a variable is      an array in JavaScript?  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>      How to check if a variable is      an array in JavaScript?  </b>    <p>      Click on the button to check       if the variable is an array  </p>    <p>Output for string:        <div class="outputString"></div>      <p>Output for number:        <div class="outputNumber"></div>      <p>Output for array:        <div class="outputArray"></div>        <button onclick="checkArray()">Click here</button>    <script type="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>
Output:
Method 3: Checking the constructor property of the variableAnother method to check a variable is an array is by checking it’s constructor with Array.Syntax:variable.constructor === ArrayThis 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><html lang="en">  <head>    <title>      How to check if a variable is      an array in JavaScript?  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>How to check if a variable is      an array in JavaScript?</b>        <p>Click on the button to check       if the variable is an array</p>        <p>Output for string:        <div class="outputString"></div>      <p>Output for number:        <div class="outputNumber"></div>      <p>Output for array:        <div class="outputArray"></div>        <button onclick="checkArray()">      Click here  </button>    <script type="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>Output:
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><html lang="en">  <head>    <title>      How to check if a variable is      an array in JavaScript?  </title></head>  <body>    <h1 style="color: green">      GeeksforGeeks  </h1>    <b>How to check if a variable is      an array in JavaScript?</b>        <p>Click on the button to check       if the variable is an array</p>        <p>Output for string:        <div class="outputString"></div>      <p>Output for number:        <div class="outputNumber"></div>      <p>Output for array:        <div class="outputArray"></div>        <button onclick="checkArray()">      Click here  </button>    <script type="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>
Output:
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
javascript-array
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?