Javascript Check if an Object Is an Array
Posted on: February 07, 2021 by Deven
In JavaScript To check if an Object Is an Array we use the static Array.isArray()
method.
const fruitNames = ['Apple', 'Mango', 'Banana', 'Grapes', 'pineapple'];
if (Array.isArray(fruitNames)) {
// Our code ends here because it is a valid array
}
isArray()
specifically checks for instances of the Array object. If we call it on a different type of collection like Map
or Set
, it returns false.
Share on social media
//