Open In App

How to check if a variable is an array in JavaScript?

Improve
Improve
Like Article
Like
Save
Share
Report

This article will show you how to check whether the given variable value is an array or not.

Method 1: Using JavaScript isArray() method

This method checks whether the passed variable is an Array object or not. 

Syntax:

Array.isArray(variableName)

It returns a true boolean value if the variable is an array and a false if it is not.

Example 1: In this example, we will check if a given variable is an array or not using the isArray() method in JavaScript. 

Javascript




function checkArray() {
    let str = 'This is a string';
    let num = 25;
    let arr = [10, 20, 30, 40];
 
    let ans = Array.isArray(str);
    console.log("Output for String: " + ans);
 
    ans = Array.isArray(num);
    console.log("Output for Number: " + ans);
 
    ans = Array.isArray(arr);
    console.log("Output for Array: " + ans);
}
 
checkArray();


Output

Output for String: false
Output for Number: false
Output for Array: true

Method 2: Using JavaScript instanceof operator

It is used to test whether the prototype property of a constructor appears anywhere in the prototype chain of an object. This can be used to evaluate if the given variable has a prototype of ‘Array’.

Syntax:

variable instanceof Array

Return value: The operator returns a true boolean value if the variable is the same as what is specified (here an Array) and a false if it is not. This is shown in the example below. 

Example: In this example, we will check if a given variable is an array or not using the instanceof operator in JavaScript. 

Javascript




function checkArray() {
    let str = 'This is a string';
    let num = 25;
    let arr = [10, 20, 30, 40];
 
    let ans = str instanceof Array;
    console.log("Output for String:" + ans);
 
    ans = num instanceof Array;
    console.log("Output for Number:" + ans);
 
    ans = arr instanceof Array;
    console.log("Output for Array:" + ans);
}
 
checkArray();


Output

Output for String:false
Output for Number:false
Output for Array:true

Method 3: Checking the constructor property of the variable

Another method to check a variable is an array by checking its constructor with Array. 

Syntax:

variable.constructor === Array

This becomes true if the variable is the same as what is specified (here an Array) and false if it is not. This is shown in the example below. 

Example: In this example, we will check if a given variable is an array or not by checking the constructor property of the variable.

Javascript




function checkArray() {
    let str = 'This is a string';
    let num = 25;
    let arr = [10, 20, 30, 40];
 
    let ans = str.constructor === Array;
    console.log("Output for String:" + ans);
 
    ans = num.constructor === Array;
    console.log("Output for Number:" + ans);
 
    ans = arr.constructor === Array;
    console.log("Output for Array:" + ans);
}
 
checkArray();


Output

Output for String:false
Output for Number:false
Output for Array:true

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Last Updated : 04 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads