Which of the following will correctly check if a variable is an array in JavaScript?
a)
Array.isArray(variable)
b)
typeof variable === 'array'
c)
variable.isArray()
d)
variable instanceof Object
Answer:
a)
Array.isArray(variable)
Explanation:
The most reliable way to check if a variable is an array in JavaScript is by using Array.isArray(variable)
. This method returns true
if the variable is an array and false
otherwise.