Answered by : reagan-mcfarland
if (array === undefined || array.length == 0) { // array empty or does not exist
}
Source : | Last Update : Sat, 12 Sep 20
Answered by : vikas
arr = []; // set array=[]
//function
const empty = arr => arr.length = 0;
//example
var arr= [1,2,3,4,5];
empty(arr) // arr=[]
Source : https://1loc.dev/#compare-two-arrays-regardless-of-order | Last Update : Thu, 09 Jul 20
Answered by : code-grepper
var colors = ["red","blue","green"]; colors = []; //empty the array
Source : | Last Update : Tue, 29 Oct 19
Answered by : ilham-surya
arr = [];
Source : https://www.tutorialspoint.com/in-javascript-how-to-empty-an-array | Last Update : Sat, 05 Mar 22
Answered by : philani-sithembiso-ndhlela
let list = [1, 2, 3, 4];
function empty() { //empty your array list.length = 0;
}
empty();
Source : https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/ | Last Update : Mon, 08 Jun 20
Answered by : distinct-dotterel-lhrq2rbi87v3
A = [];
Source : https://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript | Last Update : Fri, 30 Oct 20
Answered by : philani-sithembiso-ndhlela
// define Array
let list = [1, 2, 3, 4];
function empty() { //empty your array list = [];
}
empty();
Source : https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/ | Last Update : Mon, 08 Jun 20
Answered by : gbor-deutsch
let myArray = [12 , 222 , 1000 ];
myArray.length = 0; // myArray will be equal to [].
Source : https://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript | Last Update : Fri, 27 Nov 20
Answered by : modern-mouse
A.length = 0
Source : https://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript | Last Update : Sat, 06 Jun 20
Answered by : saurav-patel
// 1) Assigning it to a new empty array
// This is the fastest way to empty an array:
a = [];
// This code assigned the array a to a new empty array. It works perfectly if you do not have any references to the original array.
// See the following example:
let b = a;
a = [];
console.log(b); // [1,2,3]
// Code language: JavaScript (javascript)
// In this example, first, the b variable references the array a. Then, the a is assigned to an empty array. The original array still remains unchanged.
// 2) Setting its length to zero
// The second way to empty an array is to set its length to zero:
a.length = 0;
// The length property is read/write property of an Array object. When the length property is set to zero, all elements of the array are automatically deleted.
// 3) Using splice() method
// The third way to empty an array is to remove all of its elements using the splice() method as shown in the following example:
a.splice(0,a.length);
// Code language: CSS (css)
// In this solution, the splice() method removed all the elements of the a array and returned the removed elements as an array.
// 4) Using pop() method
// The fourth way to empty an array is to remove each element of the array one by one using the while loop and pop() method:
while(a.length > 0) { a.pop();
}
Source : https://www.javascripttutorial.net/array/4-ways-empty-javascript-array/ | Last Update : Mon, 12 Sep 22