Skip to content
Home » How to check if an array is empty, NULL, or undefined in jQuery?

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 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

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

  1. Pingback: PHP Shorthand If Else (Ternary Operator) - PHPCODER.TECH

  2. Pingback: Locate empty directories in windows using CMD- PHPCODER.TECH

Leave a Reply

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