This article will show you how to check whether the given variable value is an array or not.
Table of Content
Method 1: Using JavaScript isArray() method
isArray() 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.
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
The instanceof operator 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.
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
The constructor property is the 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.
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
Method 4: Using Object.prototype.toString.call()
The Object.prototype.toString.call() method can be utilized to determine the type of an object. If the result of this method call is '[object Array]', then the variable is an array.
Syntax:
Object.prototype.toString.call(variableName) === '[object Array]'
Example:
let str = 'This is a string';
let num = 25;
let arr = [10, 20, 30, 40];
let strResult = Object.prototype.toString.call(str) === '[object Array]' ? 'true' : 'false';
console.log("Output for String: " + strResult);
let numResult = Object.prototype.toString.call(num) === '[object Array]' ? 'true' : 'false';
console.log("Output for Number: " + numResult);
let arrResult = Object.prototype.toString.call(arr) === '[object Array]' ? 'true' : 'false';
console.log("Output for Array: " + arrResult);
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.