Sova
0
Q:

javascript is array empty

if (typeof array !== 'undefined' && array.length === 0) {
    // the array is defined and has no elements
}
4
if (array && !array.length) {
    // array is defined but has no element
}
9
arr = [];   // set array=[]

//function
const empty = arr => arr.length = 0;
//example
var arr= [1,2,3,4,5];
empty(arr) // arr=[]
10
var colors = ["red","blue","green"];
    colors = []; //empty the array
9
var colors=[];
if(!colors.length){
	// I am empty
}else{
	// I am not empty
}
2
// Variable array for a wider scope:
var arrayName = [];
// Local scope variable array:
let arrayName = [];
2
if (typeof image_array !== 'undefined' && image_array.length > 0) {
    // the array is defined and has at least one element
}
1
Array.isArray(emptyArray) && emptyArray.length
0
array.length = []
1
// define Array
let list = [1, 2, 3, 4];
function empty() {
    //empty your array
    list = [];
}
empty();
2

New to Communities?

Join the community