Home ยป Check If Array Is Empty Or Undefined In JavaScript

Check If Array Is Empty Or Undefined In JavaScript

Last updated on December 24, 2020 by

Table of Contents
METHOD: 01 Use Typeof Operator and Length Property
METHOD: 02 Use Array.isArray(), Typeof Operator and Length Property (Recommended)

One of the most frequently searched questions is how to check if an array is empty or undefined in JavaScript. So let’s learn some of the appropriate ways to do it. Are you ready? Let’s dive into it.

METHOD: 01 Use Typeof Operator and Length Property

The following method is very commonly used by numerous developers. So let’s check an array is empty or not.

This method has one drawback. If you pass any non-empty string then it will pass the test which is wrong, so for that we have to use Method 2 but to understand Method 2, you should try & test Method 1.

<script type="text/javascript">

let devices = [ 'keyboard', 'laptop', 'desktop', 'speakers', 'mouse' ];

// let devices = [ ]; // Try uncommenting this for testing

 if ( typeof devices !== 'undefined' && devices.length > 0 ) {
	alert('array is not empty');
 } else {
	alert('array is empty');
 }

</script>

On the above example, we have added typeof operator to check the data type of variables so if your array is not defined, then it will return an undefined string that means your array is not present into the script but if it exists then it will return an array or object as a string. You can test typeof operator like this alert(typeof devices);, it will alert the object or array string.

Then we have added array.length > 0 to check the length (number of elements) of the array which ensure that an array is not empty or not null.

Testing

To test the above example with an empty array let devices = []; and then try to remove whole devices array that will produce an undefined error, but the above example will pass that too and return with “array is empty”.

For further testing, you can also remove the typeof condition and test it with your browser’s console. It will generate the undefined error.

METHOD: 02 Use Array.isArray(), Typeof Operator and Length Property (Recommended)

In this method, we will use JavaScript’s inbuilt function Array.isArray() to check whether the provided variable is an array or not which will overcome the Method 1’s drawback.

Syntax:

Array.isArray(your-array)
<script type="text/javascript">

let devices = [ 'keyboard', 'laptop', 'desktop', 'speakers', 'mouse' ];

// let devices = [ ]; // Try uncommenting this for testing

 if ( typeof devices !== 'undefined' && Array.isArray(devices) && devices.length > 0 ) {
	alert('array is not empty');
 } else {
	alert('array is empty');
 }

</script>

We are recommending you to use this method. Try to play with the above example as we mentioned in Method 1 and let us know if you found anything wrong with the example so that we can improve further and update the post.

That’s it for now. We hope this article helped you to check if array is empty or undefined in JavaScript.

Additionally, read our guide:

  1. How To Check If An Element Is Hidden In jQuery
  2. jQuery Form Submit With Examples
  3. How To Detect IE Browser In JavaScript
  4. Simple Date formatting In JavaScript
  5. AJAX PHP Post Request With Example
  6. Difference Between == vs === in JavaScript
  7. How To Remove A Specific Item From An Array In JavaScript
  8. How To Check Array Contains A Value In JavaScript

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance ๐Ÿ™‚ Keep Smiling! Happy Coding!

 
 

Leave a Comment