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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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
How to check if the page is the home page in WordPress?WordPressSelf-Driving and Intelligent NetworksMiscellaneousThe SQL UNION OperatorSQL/MySQLWhat’s a repository on Github?MiscellaneousLearn to create profile card using HTML and CSSCSSHow to make flexbox items of the same size?CSSHow to show widgets on the Appearance tab in WordPress?WordPressWhat are Stored Procedures for SQL Server?SQL/MySQLHow to load variables, functions, and mixins from another module in SCSS?SCSSHow to make a curtain slider using jQuery and CSS?CSSJavaScript arrays: a separate data type or Objects?JavaScriptHow to add a select dropdown in React?React