Coding Ninjas Logo

Home > JavaScript Questions > How to empty an array in JavaScript?

How to empty an array in JavaScript?



Answer: There are multiple ways of emptying an array in JavaScript. Four of the most important ones are:

By assigning an empty array:
var array1 = [1, 22, 24, 46];
array1 = [];

By assigning array length to 0:
var array1 = [1, 22, 24, 46];
array1.length=0;

By popping the elements of the array:
var array1 = [1, 22, 24, 46];
while(array1.length > 0) {
array1.pop();
}

By using the splice array function:
var array1 = [1, 22, 24, 46];
array1.splice(0, array1.length)


Similar Questions