Check-if-an-array-is-empty-by-array-length-property

To check if an array is empty, NULL, or undefined in jQuery and JavaScript, we use two major functions,

  1. jQuery.isEmptyObject(arrayVariable)
  2. length property

To check, if an array is undefined we use typeof JS operator. And by using jQuery of JS length property we can check the length of the array, to check if it is empty or not.

Here we can check multiple examples, that show how we can check if an array is empty.

And if not how can I loop through the complete array of data with their keys?

Example-1 By using the JS length property

In the above example, first, we have defined a non-empty array, and by using jQuery each function loop through all the array values.

At last, by using the array length property we have checked whether an array is empty or not.

Output:

Check if an array is empty by the array length property

Example-2 By using isEmptyObject jQuery function

Here also we have just defined two arrays, one is non-empty, and the second is an empty array.

And by using jQuery.isEmptyObject the function we can check if an array is empty or NULL.

Output:

false
true

Example-3 By using if-else condition and typeof JS operator

Output:

myArray2 is empty.
myArray3 is empty.
myArray4 is empty.

Conclusion

In this article, we have learned complete knowledge about how we can check if an array is empty, NULL, or undefined programmatically.

Also, you can check the complete concept via multiple running examples. You can run those examples on JS online compilers like JSFiddle – Code Playground.

I hope you understand the complete concept and implementation. Let me know if you are facing any issues.

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

2 thoughts on “How to check if an array is empty, NULL, or undefined in jQuery?”

Leave a Reply

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