Sometimes, we want to detect if a variable is an array with JavaScript.
In this article, we’ll look at how to detect if a variable is an array with JavaScript.
How to detect if a variable is an array with JavaScript?
To detect if a variable is an array with JavaScript, we use the Array.isArray
method.
For instance, we write
const isArray = Array.isArray([]);
to check if the empty array is an array with Array.isArray
.
It should return true
since it’s an array.
Otherwise, it’ll return false
.
Conclusion
To detect if a variable is an array with JavaScript, we use the Array.isArray
method.