{"id":1199,"date":"2023-12-26T17:00:00","date_gmt":"2023-12-26T17:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=1199"},"modified":"2024-01-15T09:53:51","modified_gmt":"2024-01-15T09:53:51","slug":"empty-array-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/empty-array-javascript\/","title":{"rendered":"Empty an Array in JavaScript | 4 Methods (with code)"},"content":{"rendered":"\n<p>In the coding world, we make use of arrays in almost every situation. But sometimes we need to clear an array, i.e. empty its contents. In this article, we will learn how to empty an array in JavaScript.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong> 4 Methods to Empty an Array in JavaScript<\/strong><\/h2>\n\n\n\n<p>Before moving to the main topic, we need to revise what an array is. Arrays are linear data structures that store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements. <\/p>\n\n\n\n<p>Emptying an array means removing all elements from the array which makes it an array with a length of zero. The empty array will now have no values.<\/p>\n\n\n\n<p>An empty\/clear array can be necessary when you want to reset the array and remove all its elements. It can also be beneficial for memory management because there might be situations when some array holds a large amount of data and we no longer need it.<\/p>\n\n\n\n<p>Let us look into various techniques to clear an array in JavaScript:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Re-Assignment of the Array<\/strong><\/h3>\n\n\n\n<p><strong>The simplest way to empty an array in JavaScript is to re-assign the original array to a new empty array that has no elements.<\/strong> <\/p>\n\n\n\n<p>The following examples show how to do it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet arr=[1,2,3,4,5];\n\n\/\/reassigning the array to an empty array\narr=[]\n\n\/\/printing the array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>&#91; ]<\/code><\/pre>\n\n\n\n<p>The output is an empty array, hence we have obtained the desired output.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Modifying the Length of the Array<\/strong><\/h3>\n\n\n\n<p>The length of the array specifies the number of elements that it contains. If the array has no elements, its length will be zero. So, in this technique, we update the length of the array to zero. Let us see an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet arr=[1,2,3,4,5];\n\n\/\/printing the original length\nconsole.log(arr.length)\n\n\/\/printing the original array\nconsole.log(arr);\n\n\/\/modifying the length of an array\narr.length=0\n\n\/\/printing the modified length\nconsole.log(arr.length);\n\n\/\/printing the modified array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>5\n&#91; 1, 2, 3, 4, 5 ]\n0\n&#91; ]\n<\/code><\/pre>\n\n\n\n<p>In this example, we have taken an array with five(5) elements. Hence, the original length is 5. Then, we update the length of the array to zero(0). This modifies the original array and its length. Array becomes empty(gets cleared) and the length of this empty array is zero. We have obtained the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using the pop() Method<\/strong><\/h3>\n\n\n\n<p>The pop() method is used to remove the last element from the array. So, we can use the pop() method in an array till its length becomes zero. This will clear the array.\u00a0Here is how to do it:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet arr=[1,2,3,4,5];\n\n\/\/using the pop() method\nwhile(arr.length&gt;0){\n    \/\/popping elements till length becomes zero\n    arr.pop()\n}\n\n\/\/printing the new length\nconsole.log(arr.length);\n\n\/\/printing the new array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>0\n&#91; ]\n<\/code><\/pre>\n\n\n\n<p>We have used a while loop to pop the array elements till its length becomes zero. Hence, one by one the last element of the array gets removed. This clears the array. The array becomes empty and hence its length becomes zero. We have obtained the desired result.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Using the Splice() Method<\/strong><\/h3>\n\n\n\n<p>The splice() method in JavaScript removes elements from an array by specifying the starting index and the number of elements to be removed. We can specify the elements to be removed as the total number of elements in the array and can provide the starting index as zero(0). This will remove all the elements from the array, i.e. it will become empty.\u00a0<\/p>\n\n\n\n<p>Check the example below:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an array\nlet arr=[1,2,3,4,5];\n\n\/\/using the splice method\narr.splice(0,arr.length);\n\n\/\/printing the new length\nconsole.log(arr.length);\n\n\/\/printing the new array\nconsole.log(arr);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>0\n&#91; ]\n<\/code><\/pre>\n\n\n\n<p>We have used the splice method to remove all the elements from the array. The array becomes empty and hence its length becomes zero. We have obtained the desired result.<\/p>\n\n\n\n<p>Another interesting question asked in technical interviews is to <a href=\"https:\/\/favtutor.com\/articles\/reverse-array-javascript\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/reverse-array-javascript\/\">reverse an array in JavaScript<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have seen various techniques to clear an array in JavaScript. We can re-assign the array to an empty array, modify its length, use the pop() method till it becomes empty, or use the splice() method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn some simple methods to clear or empty an array in javascript using pop, and splice functions or by re-assigning or modifying the array.<\/p>\n","protected":false},"author":9,"featured_media":1201,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11],"class_list":["post-1199","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=1199"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1199\/revisions"}],"predecessor-version":[{"id":1400,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/1199\/revisions\/1400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1201"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}