How to Check if a Variable is an Array in JavaScript?



To check if a variable is an array in Javascript is essential to handle data appropriately. We will discuss three different approaches to check if a variable is an array or not.

We are having an array and a string, and our task is to check if a variable is an array in JavaScript.

Approaches to Check if a Variable is an Array

Here is a list of approaches to check if a variable is an array in JavaScript which we will be discussing in this article with stepwise explaination and complete example codes.

Using isArray() Method

To check if a variable is an array in JavaScript, we have used isArray() method. It returns a boolean value as result, either true or false.

  • We have used two div elements to display the array and output using getElementById() and innerHTML property. We have also added a button which triggers checkArray() function upon clicking.
  • We have initialized and stored an array in arr and a string in variable str.
  • Then we have used isArray() method on both, array and string and stored this result in res1 and res2 respectively.

Example

Here is a complete example code implementing above mentioned steps to check if a variable is an array in JavaScript using isArray() method.

 <!DOCTYPE html> <html lang="en"> <head> <title> To Check if a Variable is an Array in JavaScript </title> </head> <body> <h2> To Check if a Variable is an Array in JavaScript </h2> <p> In this article, we have used <strong>isArray()</strong> method to check if a variable is an array in JavaScript. </p> <button onclick="checkArray()">Check</button> <br><br> <div id="array"></div> <div id="result"></div> <script> let arr = [1, 2, 3, 4, 5]; let str = "Tutorialspoint"; document.getElementById('array') .innerHTML= "Variable: " +arr +"<br>" +"Variable: " +str; function checkArray() { let res1 = Array.isArray(arr); let res2 = Array.isArray(str); document.getElementById("result") .innerHTML = "Is it array?: " +"<br>" +res1 +"<br>" +res2; } </script> </body> </html> 

Using instanceof Operator

In this approach we have used instanceof Array operator to check if a variable is an array in JavaScript.

  • We have used two div elements to display the array and output using getElementById() and innerHTML property. We have also added a button which triggers checkArray() function upon clicking.
  • We have initialized and stored an array in arr and a string in variable str.
  • Then we have used instanceof Array operator on both, array and string and stored this result in res1 and res2 respectively.

Example

Here is a complete example code implementing above mentioned steps to check if a variable is an array in JavaScript using instanceof operator.

 <!DOCTYPE html> <html lang="en"> <head> <title> To Check if a Variable is an Array in JavaScript </title> </head> <body> <h2> To Check if a Variable is an Array in JavaScript </h2> <p> In this article, we have used <strong>instanceof</strong> operator to check if a variable is an array in JavaScript. </p> <button onclick="checkArray()">Check</button> <br><br> <div id="array"></div> <div id="result"></div> <script> let arr = [1, 2, 3, 4, 5]; let str = "Tutorialspoint"; document.getElementById('array') .innerHTML= "Variable: " +arr +"<br>" +"Variable: " +str; function checkArray() { let res1 = arr instanceof Array; let res2 = str instanceof Array; document.getElementById("result") .innerHTML = "Is it array?: " +"<br>" +res1 +"<br>" +res2; } </script> </body> </html> 

Checking constructor Property of Variable

In this approach to check if a variable is an array in JavaScript we have checked constructor property of variable.

  • We have used two div elements to display the array and output using getElementById() and innerHTML property. We have also added a button which triggers checkArray() function upon clicking.
  • We have initialized and stored an array in arr and a string in variable str.
  • Then we have checked the constructor property of array and string using arr.constructor === Array and str.constructor === Array; and stored this result in res1 and res2 respectively.

Example

Here is a complete example code implementing above mentioned steps to check if a variable is an array in JavaScript by checking constructor property of variable. It displays true when the variable is same as what we specified.

 <!DOCTYPE html> <html lang="en"> <head> <title> To Check if a Variable is an Array in JavaScript </title> </head> <body> <h2> To Check if a Variable is an Array in JavaScript </h2> <p> In this article, we have used <strong>constructor</strong> property of variable to check if a variable is an array in JavaScript. </p> <button onclick="checkArray()">Check</button> <br><br> <div id="array"></div> <div id="result"></div> <script> let arr = [1, 2, 3, 4, 5]; let str = "Tutorialspoint"; document.getElementById('array') .innerHTML= "Variable: " +arr +"<br>" +"Variable: " +str; function checkArray() { let res1 = arr.constructor === Array; let res2 = str.constructor === Array; document.getElementById("result") .innerHTML = "Is it array?: " +"<br>" +res1 +"<br>" +res2; } </script> </body> </html> 

Conclusion

In this article we discussed how check if a variable is an array in JavaScript using three different approaches which are: by using isArray() method, instanceof operator and by checking constructor property of variable.

Updated on: 2024-11-20T17:11:18+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements