Let's consider a JS array,
const authors = ['Param', 'Joshua'];
This is obviously an array, but if you need to check whether the data is an array or not. How do you do that check?
Using the Array.isArray
method, it is easy to find out whether the value of a variable is an array or not.
console.log(Array.isArray(authors)); // true
If you pass a non-array value to the method, then it will return false
.
console.log(Array.isArray(true)); // false
console.log(
Array.isArray({
firstName: 'Param',
lastName: 'Harrison'
})
); // false
Top comments (0)