Yogesh Chauhan's Blog

How to Recognize an Array in JavaScript?

in JavaScript on December 3, 2020

The problem is that the JavaScript operator typeof returns “object”:

var numbers = [2, 3, 4]; 

typeof numbers;



As we have already seen in this post, JavaScript Arrays: A Separate Data Type Or Objects?JavaScript arrays are objects.

Method 1

We can use Array.isArray() method which was introduced in ECMAScript 5.

var numbers = [2, 3, 4]; 


// true

👎 This method is not supported in older browsers.

Method 2

We can make a our function and use constructor property to know whether it’s an array.

var numbers = [2, 3, 4]; 

function isArray(x) {
  return x.constructor.toString().indexOf("Array") > -1;


// true

Let’s understand it.

The constructor property returns an array’s constructor function:

var numbers = [2, 3, 4]; 


// [Function: Array]

which we convert to a string and get the index of word “Array”. If it’s the array then the word “array” will be there and will return positive value.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Sequence generator (range) using JavaScript Array.from()JavaScriptHow to select multiple values in React dropdown?ReactWhat is an API?MiscellaneousWhat are Identifiers in JavaScript?JavaScriptGlobal JavaScript methods to convert variables to numbersJavaScriptHow to create a simple drop down menu using JavaScript and CSS?CSSWP_Query Class in WordPressWordPressHow destructuring works in React?ReactHow to float an image around texts?CSSHow to create rotating texts using JavaScript and CSS?CSSA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSSWhat is PostgreSQL? How similar or different it is from SQL?Postgres