Clear or Reset a JavaScript Array
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());
4.13
8
let arr = [3.14, 7];
arr = [];//make sure there is no other reference
Thank you!
8
0
0
9
var ar = [1, 2, 3, 4, 5, 6];
//do stuffar = [];
//a new, empty array!
Thank you!
9
0
4.3
10
A = [];
Thank you!
10
0
Are there any code examples left?
New code examples in category Javascript