To check if an array is not empty in JavaScript, you can use the length
property of the array.
In this example,
numbers
with some elements.notEmpty
.let numbers = [1, 2, 3];
let notEmpty = numbers.length !== 0;
console.log(`Array is not empty: ${notEmpty}`);
Array is not empty: true
In this example,
emptyArray
.notEmpty
.let emptyArray = [];
let notEmpty = emptyArray.length !== 0;
console.log(`Array is not empty: ${notEmpty}`);
Array is not empty: false
In this tutorial, we learned How to check if an Array is Not Empty in JavaScript language with well detailed examples.