How to Empty an Array in JavaScript? Last Updated : 11 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To empty an array in JavaScript, we can use the array literal. We can directly assign an empty array literal to the variable, it will automatically remove all the elements and make the array empty.1. Empty Array using Array LiteralWe can empty the array using the empty array literal syntax. Syntaxarr = [ ]; JavaScript // Given array let a = [ 10, 20, 30, 40, 50 ]; // Assign new array to make it empty a = []; // Display the updated array console.log("Updated Array: ", a) OutputUpdated Array: [] 2. Using array.length PropertyThe JavaScript array.length property is used to set or return the length of the array.Syntaxarr.length = 0; JavaScript // Given array let a = [ 10, 20, 30, 40, 50 ]; // Empty the array by setting its length to 0 a.length = 0; // Display Updated array and its length console.log("Updated Array: ", a); console.log("Length of Updated Array: ", a.length); OutputUpdated Array: [] Length of Updated Array: 0 3. Using array.splice() MethodThe JavaScript array.splice() method is used to remove elements from certain index and add new elements to the array.Syntaxarr.splice(0); It will start removing elements from 0 index and as the number of elements is not given it will remove all elements present in the array. JavaScript // Given array let a = [ 10, 20, 30, 40, 50 ]; // Remove elements using array.splice() method a.splice(0); // Display the updated array console.log("Updated Array: ", a) OutputUpdated Array: [] 4. Using array.pop() MethodThe JavaScript array.pop() method is used to remove the elements one by one from the array.Syntaxarr.pop(); JavaScript // Given array let a = [ 10, 20, 30, 40, 50 ]; // While loop to iterate while( a.length > 0){ // Remove the last element in // each iteration a.pop() } // Display the updated array console.log("Updated Array: ", a) OutputUpdated Array: [] Note: Array clearing techniques in JavaScript is essential for effective data management. Comment More infoAdvertise with us Next Article How to Empty an Array in JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to Truncate an Array in JavaScript? Here are the different methods to truncate an array in JavaScript1. Using length PropertyIn Array.length property, you can alter the length of the array. It helps you to decide the length up to which you want the array elements to appear in the output.JavaScriptconst n = [1, 2, 3, 4, 5, 6]; n.length 4 min read Check if an array is empty or not in JavaScript These are the following ways to check whether the given array is empty or not:1. The length Property - mostly usedThe length property can be used to get the length of the given array if it returns 0 then the length of the array is 0 means the given array is empty else the array have the elements.Jav 1 min read How to initialize an array in JavaScript ? Initializing an array in JavaScript involves creating a variable and assigning it an array literal. The array items are enclosed in square bracket with comma-separated elements. These elements can be of any data type and can be omitted for an empty array.Initialize an Array using Array LiteralInitia 3 min read Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J 3 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read Like