YogeshChauhan . com

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Here’s what we can do with PHP date() functionPHPWhat is Git?MiscJavaScript String fromCharCode() MethodJavaScriptA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSSHow to Install PHP Laravel on MacOS Catalina?PHPCSS backface-visibility PropertyCSSAvoid using new Array() in JavaScriptJavaScriptWordPress: How to query all posts from custom post type and display them in a list?WordPressConfiguring Modules with @forward rule in SCSS (Sass)SCSSHow to load variables, functions, and mixins from another module in SCSS?SCSSHow can one check to see if a remote file exists using PHP?PHPHow to Pop an Alert Box in PHP?JavaScript