InterviewHubPRO
Section Overview
Q445

Question 445 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

85%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

How to verify if a variable is an array?

It is possible to check if a variable is an array instance using 3 different ways,

  1. Array.isArray() method:

    The Array.isArray(value) utility function is used to determine whether value is an array or not. This function returns a true boolean value if the variable is an array and a false value if it is not.

javascript
    const numbers = [1, 2, 3];
    const user = { name: "John" };
    Array.isArray(numbers); // true
    Array.isArray(user); //false
  1. instanceof operator:

    The instanceof operator is used to check the type of an array at run time. It returns true if the type of a variable is an Array other false for other type.

javascript
    const numbers = [1, 2, 3];
    const user = { name: "John" };
    console.log(numbers instanceof Array); // true
    console.log(user instanceof Array); // false
  1. Checking constructor type:

    The constructor property of the variable is used to determine whether the variable Array type or not.

javascript
    const numbers = [1, 2, 3];
    const user = { name: "John" };
    console.log(numbers.constructor === Array); // true
    console.log(user.constructor === Array); // false