
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
To Check if a Variable is an Array in JavaScript To Check if a Variable is an Array in JavaScript
In this article, we have used isArray() method to check if a variable is an array in JavaScript.
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.
To Check if a Variable is an Array in JavaScript To Check if a Variable is an Array in JavaScript
In this article, we have used instanceof operator to check if a variable is an array in JavaScript.
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.
To Check if a Variable is an Array in JavaScript To Check if a Variable is an Array in JavaScript
In this article, we have used constructor property of variable to check if a variable is an array in JavaScript.
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.