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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.