JavaScript Periksa apakah array kosong
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 && !array.length) {
// array is defined but has no element
}
if (Array.isArray(array) && array.length) {
// array exists and is not empty
}
if(typeof array != 'undefined' && array.length > 0){
// array has elements
}
var colors=[];
if(!colors.length){
// I am empty
}else{
// I am not empty
}