HDTuto.com

JQuery Check If Variable is Empty or Null or Undefined Example



This tutorial will give you example of jquery check if array is empty. it's simple example of jquery check if array is empty. Here you will learn jquery check if array is empty or null or undefined. you can see check if empty array jquery.

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? - HDTuto.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...




May 9, 2020 | Category : JQuery



Blog Search