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

In JavaScript, you can check if a variable is an array using the Array.isArray() method. This method returns true if the passed argument is an array, and false otherwise.

Here’s an example:

const _arr = [1, 2, 3];

if (Array.isArray(_arr)) {
  console.log("I am an array!");
} else {
  console.log("I am not an array!");
}

Output:

I am an array!

In this example, the Array.isArray() method is used to check if _arr is an array. Since _arr is indeed an array, the method returns true, and the output is “I am an array!“.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: