How to check if an array is empty or exist in JavaScript

If you are using arrays in your project and would like to use the array method, we suggest you check if the array is empty or exists. So here we will show you the possible ways to verify the Array in JavaScript.

To check if an array exists and length is greater than zero

We will recommend you to use the Array.isArray() and length property to verify an array.

Let’s also look at the following attempts.

1. Truthy check

This is true for all truthy values, like 1, 'a', { }. This result is not wanted.

2. Array check

This checks only if myArr is an array, but not the length of it. Empty arrays return true, which is not wanted.

3. Length check

This works only for objects that may have a property of length, which have a truthy value.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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