Splice Method Replace Array Element
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)
let array = ["Hi", "Js", "Startup"];
Now, we check the length of array using length property of an array.
Any idea what should be the output above statement ?
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.
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 –
- JS Quiz – what really is the array index value
- JS Quiz – does object hasOwnProperty really return boolean
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.