Categories
JavaScript Answers

How to detect if a variable is an array with JavaScript?

Spread the love

Sometimes, we want to detect if a variable is an array with JavaScript.

In this article, we’ll look at how to detect if a variable is an array with JavaScript.

How to detect if a variable is an array with JavaScript?

To detect if a variable is an array with JavaScript, we use the Array.isArray method.

For instance, we write

const isArray = Array.isArray([]);

to check if the empty array is an array with Array.isArray.

It should return true since it’s an array.

Otherwise, it’ll return false.

Conclusion

To detect if a variable is an array with JavaScript, we use the Array.isArray method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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