How do you check if a variable is an array in JavaScript?

How do you check if a variable is an array in JavaScript?

a) Array.isArray(variable)
b) variable.isArray()
c) typeof variable === “array”
d) variable instanceof Array

Answer:

a) Array.isArray(variable)

Explanation:

The method Array.isArray() is used to determine whether a variable is an array. It returns true if the variable is an array, and false otherwise.

Example:


let fruits = ["Apple", "Banana"];
console.log(Array.isArray(fruits)); // Output: true
    

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Leave a Comment

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

Scroll to Top