JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – does the splice method replace array element

JavaScript Quiz – does the splice method replace array element

Splice Method Replace Array Element

In javascript, the array splice method replaces the array element based on the parameter passed to the splice method. The splice method also modifies the original array.

The splice() method adds/removes items to/from an array, and returns the removed item(s).

Splice Method Syntax

array.splice(index, totalItem, item1, ....., itemX)

For more detail about javascript splice method click here.

JavaScript Question

In this javascript quiz, does the splice method replace array element based on parameter pass to splice? Splice add/remove the array element.

First, we declare an variable array using let keyword and assign an javascript array to it. which contain string only as element.

let array = ["Hi", "Js", "Startup"];

Now, we use splice method and pass 3 parameter which are 1, 1 and “JavaScript”.

array.splice(1, 1, "JavaScript");

Now, we check the length of array using length property of an array.


Any idea what should be the output above statement ?

JavaScript Quiz Test

let array = ["Hi", "Js", "Startup"];

array.splice(1, 1, "JavaScript");

console.log(array.length); // output => ?

Answer – 3

Click To Run Code


Now, you know the answer is 3 but why doesn’t splice add or remove the element. So, it should be 2 or 4. Let me explain.

As it’s correct that splice method either adds or removes but it can do both the thing at some time which said to be replaced. So, in the above situation, we remove one element and add one element in the same position.

So, if you just take an output of array value then you can find the reason of it.

["Hi", "JavaScript", "Startup"]

At last, That’s why we get an answer “3“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JS Quiz – what really is the array index value
  2. JS Quiz – does object hasOwnProperty really return boolean
  3. JavaScript Quiz – does class typeof operator give class
  4. JavaScript Quiz – array map converts an element value with parseInt


To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
Share via
Copy link