isArray() – Check if given Value is an Array
Array.isArray(value)
Array
is Array classvalue
is an object which has to be checked
isArray() is a static method of Array class. therefore we can call this method directly using Array
class name
isArray() method checks if the given value
is an array
if given value
is an array object, then isArray() returns true
, else it returns false
Examples
1. check if given value is array [positive scenario]
const value = [2, 5, 8, 10];
if ( Array.isArray(value) ) {
console.log('given value is an array');
} else {
console.log('given value is not an array');
}
2. check if given value is array [negative scenario]
const value = 'apple';
if ( Array.isArray(value) ) {
console.log('given value is an array');
} else {
console.log('given value is not an array');
}