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 deep flatten an array in JavaScript? In this article, we will learn how to deep flatten an array in JavaScript. The flattening of an array is a process of merging a group of nested arrays present inside a given array. Deep flattening means that the array would be completely flattened. Example: Input: [1,2,3,4,5,[6,[7,8,9]]] Output: [1, 2 min read 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 JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po 3 min read JavaScript - Delete an Item From an Array In Javascript, we do not have any array.remove() method for deleting the element. Here are the various methods we can delete an item from an array using JavaScript.Different Ways to Delete an Item From an Array Using JavaScript1. Using for loop and push() MethodThe push() method will not mutate the 4 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja 6 min read Like