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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
The Lending Club Analysis using Logistic Regression and Random Forest in RStudioMiscellaneousWhat’s a repository on Github?MiscellaneousWhat is Prototypal Inheritance in JavaScript?JavaScriptHow to Install PHP composer in Mac OS Catalina?PHPFULL OUTER JOIN in PostgresPostgresUse eq() method in jQueryjQueryHow to modify the latest post array using get_posts() in WordPress?WordPressHow to convert a function component into a class in React?ReactDynamically generate names in SCSS (animation example)SCSSHow to change style of nth-child using jQuery?jQueryPHP Variables ScopePHPHow to use a Subquery to Insert Multiple Rows in SQL Table?SQL/MySQL