How to check if an array is empty, NULL, or undefined in jQuery?

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 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 data with their keys.

Example-1 By using 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 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 function we can check 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 had 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 compiler 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
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.
Posts created 188

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top