3 ways to tell if a variable is an Array in JavaScript
There are several ways of checking if a variable is an array or not. The best solution is the one you have chosen.
Method #1
variable.constructor === Array
This is the fastest method on Chrome, and most likely all other browsers. All arrays are objects, so checking the constructor property is a fast process for JavaScript engines.
If you are having issues with finding out if an objects property is an array, you must first check if the property is there.
Method #2
variable.prop && variable.prop.constructor === Array
Lastly, another way to tell if a variable is an Array is as so.
Method #3
Array.isArray(variable)
Amazon Links to “Must-Haves” as a WFH Software Engineer 🙂💻
24-inch budget-friendly monitor, Portable monitor, Laptop stand, Waterproof desk mat, Keyboard cleaning gel, Six-outlet wall charger and surge protector,, Wi-Fi extender, Blue-light–blocking glasses, Memory foam gel pad, Foam footrest
Courses to learn more about datastructures/algorithms ✍️
Courses to learn more about databases 👓
DB Foundations, Data Scientists specialization
Courses to learn more about React 🧢
Full-Stack WebDev, Front-End WebDev with React, IBM Full Stack