ItSolutionStuff.com

How to Check Array Is Empty Or Null In Javascript?

By Hardik Savani • July 4, 2023
Javascript jQuery

If need for jquery check if array is empty or undefined then i will help you. you can easily check if array is empty or not in javascript. we will use simple if condition and length of array with checking. so we can easily check if array is empty null undefined in javascript.

Here i will give you simple example with multiple cases so you can use any one from there. i will suggest you that you can use this one as bellow simple code:

if (myArray && myArray.length > 0) {

console.log('myArray is not empty.');

}else{

console.log('myArray is empty.');

}

You can also see bellow full example for checking jquery array if empty. So let's bellow code and also check output as i attached bellow:

Example:

<!DOCTYPE html>

<html>

<head>

<title>How to check if array is empty or null or undefined in javascript? - ItSolutionStuff.com</title>

</head>

<body>

<script type="text/javascript">

/*

Basic Checking for Jquery Array

*/

var myArray = [1, 2, 3];

if (myArray && myArray.length > 0) {

console.log('myArray is not empty.');

}else{

console.log('myArray is empty.');

}

/*

Basic Checking with empty array for Jquery Array

*/

var myArray2 = [];

if (myArray2 && myArray2.length > 0) {

console.log('myArray2 is not empty.');

}else{

console.log('myArray2 is empty.');

}

/*

Basic Checking with undefined array for Jquery Array

*/

if (typeof myArray3 !== 'undefined' && myArray3.length > 0) {

console.log('myArray3 is not empty.');

}else{

console.log('myArray3 is empty.');

}

/*

Basic Checking with null array for Jquery Array

*/

var myArray4 = null;

if (myArray4 && myArray4.length > 0) {

console.log('myArray4 is not empty.');

}else{

console.log('myArray4 is empty.');

}

</script>

</body>

</html>

Output:

myArray is not empty.

myArray2 is empty.

myArray3 is empty.

myArray4 is empty.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Convert Array to Object in Javascript?

Read Now →

How to Convert Object to String in Javascript?

Read Now →

How to Get Last Element from Array using JQuery?

Read Now →

How to Check If Key Exists in JSON Object using JQuery?

Read Now →

How to Only Allow Numbers in a Text Box using jQuery?

Read Now →

How to Generate Random String in Javascript?

Read Now →

Autocomplete Places Search Box using Google Maps Javascript API

Read Now →

How to Set Maxlength for Textarea in Javascript?

Read Now →

JQuery Accordion using JQuery UI Example

Read Now →

How to Get First Element of Array in Javascript?

Read Now →

How to Get Last Element of Array in Javascript?

Read Now →

How to Redirect Another Web Page in JQuery?

Read Now →

How to Check Undefined, Empty and Null in JQuery?

Read Now →