Which of the following will correctly check if a variable is an array in JavaScript?

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.

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top