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