0
Q:

javascript clear array

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
A.splice(0,A.length)
0
let aray = [1,2,3,4,5,6,7,8,9,10];

//as with most coding there are several ways you can do anything
//see whichever works best for your scenario

//set the array to equal a blank array
array = [];

//set the array's length to 0
array.length = 0;

//using splice, starts at index 0 & removes everything up to and including that last index 
array.splice(0, array.length);

//map or loop through and shift() or pop();
//goes through the array one at a time and removes the last item each time
array.map( () => array.pop());

//goes through the array one at a time and removes the first item each time
array.map( () => array.shift());
1
// define Array
let list = [1, 2, 3, 4];
function empty() {
    //empty your array
    list = [];
}
empty();
2
var cars = ["mazda","honda","tesla"];
	cars = []; // clear/empty the array
2
let list = [1, 2, 3, 4];
function empty() {
    //empty your array
    list.length = 0;
}
empty();
0
var list = [1, 2, 3, 4];
function empty() {
    //empty your array
    list.length = 0;
}
empty();
1

New to Communities?

Join the community