How to Check Array is Empty or Not in Node.js?

05-Oct-2022

.

Admin

How to Check Array is Empty or Not in Node.js?

Hello Friends,

This post is focused on how to check an array is empty or not in node.js. I would like to share with you to check an array is empty or not in nodejs. let’s discuss the check array is empty or not in node js. if you have a question about node js check the array is empty or not then I will give a simple example with a solution. Let's get started with nodejs to check array is empty or not.

This example is to check if an array is empty in nodejs. i will give simply example of check array empty or not using .length(). The length property sets or returns the number of elements in an array.

Let's start following example:

Example: 1


// create blank array

var myArray = [];

// check array empty or not

if (myArray.length === 0) {

console.log("Array is empty!")

}else{

console.log("Array is not empty!")

}

Output:

Array is empty!

Example: 2

// create array list

var myArray = ['one','two','three'];

// check array is empty or not

if (myArray.length === 0) {

console.log("Array is empty!")

}else{

console.log("Array is not empty!")

}

Output:

Array is not empty!

I hope it can help you...

#Node JS