
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 do we check if an object is an array in Javascript?
There are multiple ways to check if an object is an array in JavaScript. Let us look at some of these −
Using Array.isArray()
All modern browsers support this method.
Example
console.log(Array.isArray(undefined)) console.log(Array.isArray(null)) console.log(Array.isArray("")) console.log(Array.isArray({})) console.log(Array.isArray([])) console.log(Array.isArray([1, "hello"])) console.log(Array.isArray(new Array()))
Output
false false false false true true true
constructor check
You can use arr.constructor === Array to determine is an object is array. This doesnt work for all objects though.
Example
// This will fail: console.log(undefined.constructor === Array) // This will fail: console.log(null.constructor === Array) console.log("".constructor === Array) console.log({}.constructor === Array) console.log([].constructor === Array) console.log([1, "hello"].constructor === Array) console.log(new Array().constructor === Array)
Output
false false true true true
- Related Questions & Answers
- How do I check if an array includes an object in JavaScript?
- How do you check if a variable is an array in JavaScript?
- Can we check if a property is in an object with JavaScript?
- Check if an array object is equal to another array object in C#
- How to check if an object is an instance of a Class in JavaScript?
- Determining If an Object Is an Array in Java
- How to check if a variable is an array in JavaScript?
- How do we initialize an array within object parameters in java?
- How to check if an object is a PyTorch Tensor?
- JavaScript: How to Check if a String is a Literal or an Object?
- How to determine if JavaScript object is an event?
- How do I check if an element is hidden in jQuery?
- How to verify if a JavaScript object is an array? Explain with examples.
- Check if three consecutive elements in an array is identical in JavaScript
- Check if an array is descending, ascending or not sorted in JavaScript
Advertisements