
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to check if a variable is an array in JavaScript?
In javascript we can check whether a variable is array or not by using three methods.
1) isArray() method
The Array.isArray() method checks whether the passed variable is array or not. If the variable is an array it displays true else displays false.
Syntax
Array.isArray(variableName)
Example
<html> <body> <script type="text/javascript"> arr = [1,2,3,4,5]; str = "i love my india"; document.write( Array.isArray(arr)); document.write("</br>"); document.write( Array.isArray(str)); </script> </body> </html>
Output
true false
2) 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. In the following example the instanceof operator checks whether there exists an array prototype.
Syntax
variable instanceof Array
Example
<html> <body> <script type="text/javascript"> arr = [1,2,3,4,5]; str = "i love my india"; document.write(str instanceof Array); document.write("</br>"); document.write(arr instanceof Array); </script> </body> </html>
Output
false true
3) Checking the constructor property of the variable
It displays true when the variable is same as what we specified. Here we specified that the variable should be array. So when the variable is array this method displays true else displays false.
Syntax
variable.constructor === Array
Example
<html> <body> <script type="text/javascript"> arr = [1,2,3,4,5]; str = "i love my india"; document.write(str.constructor === Array); document.write("</br>"); document.write(arr.constructor === Array); </script> </body> </html>
Output
false true
- Related Articles
- How do you check if a variable is an array in JavaScript?
- How to check if a variable is an integer in JavaScript?
- How to check if a variable is NaN in JavaScript?
- How to check if a variable is boolean in JavaScript?
- How to check if a variable exists in JavaScript?
- How do we check if an object is an array in Javascript?
- How to check whether an array is a true array in JavaScript?
- How can I check if a JavaScript variable is function type?
- How to check if an array contains integer values in JavaScript ?
- How to check if a variable is NULL in C/C++?
- How do I check if an array includes an object in JavaScript?
- How to check a not-defined variable in JavaScript?
- How to check if an object is an instance of a Class in JavaScript?
- Check if three consecutive elements in an array is identical in JavaScript
- How to check if type of a variable is string in Python?

Advertisements