{"id":306,"date":"2023-11-10T04:02:45","date_gmt":"2023-11-10T04:02:45","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=306"},"modified":"2023-11-11T17:48:18","modified_gmt":"2023-11-11T17:48:18","slug":"array-splice-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/array-splice-javascript\/","title":{"rendered":"JavaScript Array splice() Method (with Examples)"},"content":{"rendered":"\n<p>In this article, we will be taking a deep dive into Javascript arrays and how we can modify an array, i.e. add, remove, or replace elements using the splice() method.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Arrays in JavaScript?<\/strong><\/h2>\n\n\n\n<p>Arrays are linear data structures used to store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements. An array can not be a combination of integers and characters, or integers or strings. Let us take a look at how to declare and use arrays in Javascript.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>\/\/declaring an integers array\nlet marks = &#91;98, 75, 62, 82, 97, 53];\n\n\/\/declaring a characters array\nlet grades=&#91;'A', 'B', 'A', '0'];\n\n\/\/declaring a strings array\nlet favtutors=&#91;\"John\", \"Joshua\", \"Alex\", \"Kate\"];\n\n\/\/declaring a boolean array\nlet present=&#91;true, false, false, true];<\/code><\/pre>\n\n\n\n<p>In the code above, we have made integers array marks containing all integers, a characters array grade consisting of all elements as characters, a strings array &#8216;favtutors&#8217; consisting of all strings, and a boolean present array consisting of all boolean values.<\/p>\n\n\n\n<p>In JavaScript, we do not need to specify the type of the array at the compile time as it is a dynamically typed language. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Array splice() function in JavaScript<\/strong><\/h2>\n\n\n\n<p>Splice means to unite or combine two separate elements into a whole. If we want to push some more elements to an array or delete some elements already present in the array, we splice the array. So, we can perform splicing on an array if we want to remove, add, or replace elements.<\/p>\n\n\n\n<p>To perform the splice operation, we have an inbuilt function.<strong> The array splice() method in JavaScript allows one to modify the contents of an array by removing, adding, or replacing elements.<\/strong> Note that the splice() method modifies the original array.<\/p>\n\n\n\n<p>The general syntax of the array splice() method in JavaScript is:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>array_name.splice( index, remove_count, items);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>array_name:<\/strong> Name of the array to be spliced<br><\/li>\n\n\n\n<li><strong>index:<\/strong> The index from which the array must be changed\/modified. It is a required parameter and can have positive, 0, or negative values.<br><\/li>\n\n\n\n<li><strong>remove_count:<\/strong> Specifies the number or the count of the items to be removed from the array.<br><\/li>\n\n\n\n<li><strong>items: <\/strong>Represents the list of items to be added to the array. It is an optional parameter. If no item is to be added, the parameter is simply not given in the function.<\/li>\n<\/ul>\n\n\n\n<p>Note that, this is the general syntax of the splice function. It can be implemented in various ways which we will understand in the further sections.<\/p>\n\n\n\n<p>The splice() method is a standard feature of the ECMAScript JavaScript and is supported by all modern browsers, including Google Chrome, Mozilla Firefox, Safari, and Microsoft Edge. It is also supported in older versions of Internet Explorer.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Use of splice() Method<\/strong><\/h2>\n\n\n\n<p>Let&#8217;s now learn what are the use cases of the splice() method. In this section, We will take a look at how to remove elements from various positions in an array in JavaScript using the splice() function. We can remove elements both from the start and end of an array in JavaScript.\u00a0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Removing Elements from the Start of an Array<\/strong><\/h3>\n\n\n\n<p>We will first understand how to remove elements from the beginning of an array. Let\u2019s take several examples.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Define the marks array as marks=[98, 78, 92, 83, 65]<br><\/li>\n\n\n\n<li>Say, we have to remove three elements from the marks array from index 2. Note that in the array, indices always start from 0. So the element present at index 2 = 92.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Taking parameters into consideration, here the index = 2 (index starting where we want to remove elements). As we want to remove 3 elements, count = 3.<\/li>\n<\/ul>\n\n\n\n<p>Check out the code 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 a marks array\nlet marks=[98, 78, 92, 83, 65]\n\n\/\/we want to remove the marks of 3 students after index 2, i.e. after 92\nmarks.splice(2,3);\n\n\/\/printing the modified marks array\nconsole.log(marks);<\/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; 98, 78 ]<\/code><\/pre>\n\n\n\n<p>So, clearly, 3 elements starting from index 2 (i.e 92) are removed from the array marks.&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"1024\" height=\"560\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/1.png\" alt=\"Removing Elements from the Start using splice()\" class=\"wp-image-313\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/1.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/1-300x164.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/1-768x420.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/1-750x410.png 750w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<p>Let us take another example.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now we want to remove two elements starting from index 2. We observe that the number of elements after the 2nd index (including it) is three (92, 83 &amp; 65).<br><\/li>\n\n\n\n<li>But we are only removing 2 elements after the second index. So the last element i.e. 65 is not removed and will still be in the marks array.<\/li>\n<\/ul>\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 a marks array\nlet marks=[98, 78, 92, 83, 65]\n\n\/\/we want to remove marks of 2 students(till the end) after the index 2, i.e. after 92\nmarks.splice(2,2);\n\n\/\/printing the modified marks array\nconsole.log(marks);<\/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; 98, 78, 65 ]<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"560\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/2-1024x560.png\" alt=\"Removing Elements from the Start \" class=\"wp-image-314\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/2-1024x560.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/2-300x164.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/2-768x420.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/2-750x410.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/2-1140x623.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/2.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>To print the elements removed, simply make an array and store the deleted elements in the array deletedMarks.<\/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 a marks array\nlet marks=[98, 78, 92, 83, 65]\n\n\/\/we want to remove marks of 2 students(till the end) after the index 2, i.e. after 92\nlet deletedMarks=marks.splice(2,2);\n\n\/\/printing the modified marks array\nconsole.log(marks);\n\n\/\/printing the deleted marks\nconsole.log(deletedMarks);<\/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; 98, 78, 65 ]\n&#91; 92, 83 ]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Removing Elements from the End of an Array<\/strong><\/h3>\n\n\n\n<p><strong>The negative value is used as the index parameter to remove the element from the end of an array. <\/strong>Let\u2019s have a look at some examples.<\/p>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We will remove one element from the names array from the last (the first element from the last) and hence index parameter is provided as -1.<br><\/li>\n\n\n\n<li>We have to remove just one element from the names array, so the count is given as 1.<\/li>\n<\/ul>\n\n\n\n<p><strong>Code:<\/strong><\/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 a names array\nlet names=[&quot;John&quot;, &quot;Joshua&quot;, &quot;Alex&quot;, &quot;Katie&quot;]\n\n\/\/we want to remove the 1 name from the end i.e. Katie\nnames.splice(-1, 1);\n\n\/\/printing the modified names array\nconsole.log(names);<\/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; \u2018John\u2019, \u2018Joshua\u2019, \u2018Alex\u2019 ]<\/code><\/pre>\n\n\n\n<p><strong>Example 4:\u00a0<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We will remove two elements from the names array from the second last and hence index parameter is provided as -2.<br><\/li>\n\n\n\n<li>We have to remove two elements from the names array, so the count is given as 2.<\/li>\n<\/ul>\n\n\n\n<p>Look at the code given 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 a names array\nlet names = [&quot;John&quot;, &quot;Joshua&quot;, &quot;Alex&quot;, &quot;Katie&quot;]\n\n\/\/we want to remove the 2 names from the second last index i.e. Katie\nlet deletedNames = names.splice(-2, 2);\n\n\/\/printing the modified names array\nconsole.log(names);\n\n\/\/printing the deleted names\nconsole.log(deletedNames);<\/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; \u2018John\u2019, \u2018Joshua\u2019 ]\n&#91; \u2018Alex\u2019, \u2018Katie\u2019 ]<\/code><\/pre>\n\n\n\n<p>We have obtained the desired output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Removing the Elements along with Adding Some Elements<\/strong><\/h3>\n\n\n\n<p>Let&#8217;s say, we want to remove elements from an array along with adding some other elements. We simply provide a list of elements to be added as the parameters after the count parameter. Let us take a look at 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 a marks array\nlet marks=[98, 78, 92, 83, 65]\n\n\/\/we want to remove marks of 3 students(till the end) after the index 2, i.e. after 92\n\/\/and we also want to add some elements to the array\nlet deletedMarks=marks.splice(2,3,99,100);\n\n\/\/printing the modified marks array\nconsole.log(marks);\n\n\/\/printing the deleted marks\nconsole.log(deletedMarks);<\/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; 98, 78, 99, 100 ]\n&#91; 92, 83, 65 ]<\/code><\/pre>\n\n\n\n<p>In this example, we have removed three elements after index 2 and added two other elements, i.e. 99 and 100 to the marks array. We have simply provided 99 &amp; 100 as parameters after the count (=3) parameter in the array splice function.&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"560\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/3-1024x560.png\" alt=\"Removing the Elements along with Adding Some Elements\" class=\"wp-image-315\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/3-1024x560.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/3-300x164.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/3-768x420.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/3-750x410.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/3-1140x623.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/3.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>In this section, we will take a look at how to add elements at various positions in an array in JavaScript using the splice() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding Elements at the End of an Array<\/strong><\/h3>\n\n\n\n<p>To add elements at the end of an array using the splice() method, you need to provide the starting index as the length of the array and the number of elements to remove as 0.&nbsp;<\/p>\n\n\n\n<p>Take a look at the below 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 a marks array\nlet marks=[98, 78, 92, 83, 65]\n\n\/\/adding elements at the end of an array without deleting any\nlet deletedMarks=marks.splice(4,0,99,100);\n\n\/\/printing the modified marks array\nconsole.log(marks);\n\n\/\/printing the empty deleted marks array as no element is deleted\nconsole.log(deletedMarks);<\/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; 98, 78, 92, 83, 65, 99, 100 ]\n&#91; ]<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"320\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/4-1024x320.png\" alt=\"Adding Elements at the End of an Array\" class=\"wp-image-316\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/4-1024x320.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/4-300x94.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/4-768x240.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/4-750x234.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/4-1140x356.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/4.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>We have not removed a single element and hence the array \u201cdeletedMarks\u201d is empty. We have instead added two elements 99 and 100 to the array by providing them as parameters to the splice() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding Elements at Specific Indexes<\/strong><\/h3>\n\n\n\n<p>You can also use the splice() method to add elements at specific indexes in an array. Simply provide the <strong>desired starting index<\/strong> and the number of elements to remove as 0. Here&#8217;s 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 a marks array\nlet marks=[98, 78, 92, 83, 65]\n\n\/\/adding elements at the index 2 of an array without deleting any\nlet deletedMarks=marks.splice(2,0,99,100);\n\n\/\/printing the modified marks array\nconsole.log(marks);<\/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; 98, 78, 99, 100, 92, 83, 65 ]<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"320\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/5-1024x320.png\" alt=\"Adding Elements at Specific Indexes\" class=\"wp-image-317\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/5-1024x320.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/5-300x94.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/5-768x240.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/5-750x234.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/5-1140x356.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/5.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>We have used the index=2, therefore elements 99 and 100 are added at index two and the rest of the elements are shifted to the right with none being deleted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Replacing Elements with the splice() Method<\/strong><\/h3>\n\n\n\n<p>If we think logically, Replacing just means deleting the elements at a particular index and adding some other elements at their place (the same index). So, Replacement of the elements is the combination of removal and addition of elements to the array.<\/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 a marks array\nlet marks=[98, 78, 92, 83, 65]\n\n\/\/replacing 2 elements from the index 0\nlet deletedMarks=marks.splice(0,2,99,100);\n\n\/\/printing the modified marks array\nconsole.log(marks);<\/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; 99, 100, 92, 83, 65 ]<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"320\" src=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/6-1024x320.png\" alt=\"Replacing Elements with the splice() Method\" class=\"wp-image-318\" srcset=\"https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/6-1024x320.png 1024w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/6-300x94.png 300w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/6-768x240.png 768w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/6-750x234.png 750w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/6-1140x356.png 1140w, https:\/\/favtutor.com\/articles\/wp-content\/uploads\/2023\/11\/6.png 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p>In the above example, we first provide the index parameter as 0 starting with which two elements will be deleted (as the count=2). Then, we have provided a list of elements to be added to the array. Hence, we obtain the desired output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Important Points to Keep in Mind<\/strong><\/h2>\n\n\n\n<p>First, when working with empty arrays<strong>,<\/strong> the behavior of the splice() method differs slightly. If you call splice() with no arguments, it won&#8217;t remove any elements from the array.\u00a0<\/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 empty marks array\nlet marks = [];\n\n\/\/splicing the empty array\nlet deletedMarks = marks.splice();\n\n\/\/printing both arrays\nconsole.log(marks);\nconsole.log(deletedMarks);<\/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; ]\n&#91; ]<\/code><\/pre>\n\n\n\n<p>In this example, we call splice() on an empty array. The method doesn&#8217;t remove any elements and returns an empty array.<\/p>\n\n\n\n<p>Also, if you provide an invalid index to the splice() method, splice() will attempt to perform the operation.\u00a0 If the index greater than the length of the array is provided, it will behave as an adding function and add the new elements to the array.\u00a0<\/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 nums array\nlet nums = [1, 2, 3];\n\n\/\/providing index greater than the length of the array and trying to store deleted elements\nlet del = nums.splice(5, 0, 4, 5);\n\n\/\/printing both arrays\nconsole.log(nums);\nconsole.log(del);<\/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; 1, 2, 3, 4, 5 ]\n&#91; ]<\/code><\/pre>\n\n\n\n<p>In this example, we provide an index (5) greater than the length of the numbers array. The splice() method adds the new elements (4 and 5) to the array.<\/p>\n\n\n\n<p>Finally, remember that the splice() method modifies the original array. If you want to avoid mutating the original array, you can make a copy of it before performing the splice operation. This can be done using the spread operator or the slice() method.\u00a0<\/p>\n\n\n\n<p>You can now move on to learn about the <a href=\"https:\/\/favtutor.com\/articles\/javascript-array-filter\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/javascript-array-filter\/\" target=\"_blank\" rel=\"noreferrer noopener\">filter() method in JavaScript<\/a> here.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this blog, we discussed the built-in splice() function and its various types of approaches to add, remove, and replace elements out of an array. It is very important to keep the important points in mind to use this method effectively.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Explore the JavaScript&#8217;s Array splice() method through various examples. Learn how to efficiently add, remove, and replace elements in arrays.<\/p>\n","protected":false},"author":9,"featured_media":309,"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,13],"class_list":["post-306","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-array"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/306","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=306"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":374,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/306\/revisions\/374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/309"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}