- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 whether an array is a true array in JavaScript?
In javascript, arrays are not true arrays. They are javascript objects. So when we try to know their type using typeof() operator the displayed output will be object.
Syntax
typeof(operand);
parameters - typeof() operator takes an operand and returns the data type of the operand.
In the following example even though variable 'a' is an array, the typeof() operator returns the output as object because in general every array is an object.
Example
<html> <body> <script> var a = [1,2,5,"hello"]; document.write(typeof(a)); var b = {}; document.write("</br>"); document.write(typeof(b)); </script> </body> </html>
Output
object object
Unlike typeof() operator, Array.isArray() checks whether the passed parameter is array or not. If the parameter is an array it gives out true as output else false as output.
Syntax
Array.isArray(array);
In the following example an array 'a' and an object 'b' were passed through Array.isArray() method. This method scrutinized them and displayed true and false as output respectively.
Example
<html> <body> <script> var a = [1,2,5,"hello"]; var res1 = Array.isArray(a); document.write(res1); document.write("</br>"); var b = {}; var res2 = Array.isArray(b); document.write(res2); </script> </body> </html>
Output
true false
- Related Articles
- JavaScript: How to check whether an array includes a particular value or not?
- How to check whether an array is empty using PHP?
- How to check if a variable is an array in JavaScript?
- How to check whether multiple values exist within a JavaScript array
- How to check whether a particular key exist in javascript object or array?
- How to check whether provided elements in an array have passed a specified condition or not in JavaScript?
- Program to check whether an array Is sorted and rotated in Python
- How to check whether element is in the array in Java?
- How do you check if a variable is an array in JavaScript?
- How do we check if an object is an array in Javascript?
- How to check in C# whether the string array contains a particular work in a string array?
- How to check existence of NaN keyword in an array JavaScript
- How to check if an array contains integer values in JavaScript ?
- Iterating through an array, adding occurrences of a true in JavaScript
- Set object property in an array true/false, whether the id matches with any id from another array of objects in JavaScript?
