How to check if a variable is an array in JavaScript?
isArray
Array.isArray(variable)instanceof vs. Array.isArray() (opens in a new tab)
When checking for Array instance, Array.isArray() is preferred over instanceof because it works across realms.
instanceof
variable instanceof Arrayconstructor
variable.constructor === ArrayObject.prototype.toString()
Object.prototype.toString.call(variable) === '[object Array]'References
- How to check if Variable is an Array in JavaScript | SamanthaMing.com (opens in a new tab)
- How to check if a variable is an array in JavaScript? - GeeksforGeeks (opens in a new tab)
- How do I check if a variable is an array in JavaScript? - Stack Overflow (opens in a new tab)
- Array.isArray() - JavaScript | MDN (opens in a new tab)