Ways to empty JavaScript array

There are different ways to empty an array in Javascript.

By assigning an empty array

var numbers =[1,3,5,6];
numbers =[];
By assigning array length to 0.

var letters = ["a", "b", "c"];
letters.length = 0;
By poping the elements of the array.

var randomIntegers =[1,4,5,6];
while(randomIntegers.length > 0) {
    randomIntegers.pop();
}
By using .splice()
var cities =["Mumbai","Pune"];
cities.splice(0, cities.length)

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array