javascript slice vs splice


In English, slice is used to get portion of a string without modifying the original array, for example :-
It takes 2 parameter, start and end. Very different from splice.

Say we have the following array :-

Slice 


a = [1,2,3,4,5]

a.slice(0,1)
[1] // return

a.slice(0,2)
(2) [1, 2] 

a.slice(1,2)
(2) [2, 3]

a => values stay intact

(5) [1, 2, 3, 4, 5]


Splice 

This function takes portion of the string and change original value of your array. Splice accepts

index - which position to start

howmany

optional item to be added into the list

You will also notice that value of a is changed.


a.splice(1,2)
(2) [2, 3]
a
(3) [1, 4, 5]


It is quite different function.



Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm