JavaScript prüft, ob das Array leer ist
if (typeof array !== 'undefined' && array.length === 0) {
// the array is defined and has no elements
}
Code Hero
if (typeof array !== 'undefined' && array.length === 0) {
// the array is defined and has no elements
}
if (array === undefined || array.length == 0) {
// array empty or does not exist
}
if (array && !array.length) {
// array is defined but has no element
}
if(typeof array != 'undefined' && array.length > 0){
// array has elements
}
if(array && array.length){
// not empty
} else {
// empty
}
let arr1 = [];
let arr2 = [2, 4, 6, 8, 10];
const arr1IsEmpty = !(Array.isArray(arr1) && arr1.length >0);
const arr2IsEmpty = !(Array.isArray(arr2) && arr2.length >0);
console.log(arr1); //output - true
console.log(arr2); // output - false