slice()
returns a subset of the array it is called on:
slice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3).
var a = ['a', 'b', 'c', 'd', 'e']
// var a = [ 'a' , 'b' , 'c' , 'd' , 'e' ]
// ^0 ^1 ^2 ^3 ^4(-1) ^5(0)
a.slice(0, 3) // ['a', 'b', 'c']
a.slice(2) // ['c', 'd', 'e']
a.slice(1, -1) // ['b', 'c', 'd']
a.slice(-3, -2) // ['c']
splice() is used to insert and remove elements from an array. It modifies the array in place
The first argument is an index where the inserting or removing is to begin
The second argument is how many elements to remove
The remaining arguments are elements to insert at the given index
var a = ['a', 'b', 'c', 'd', 'e'];
a.splice(4); // Returns ['e'], a is ['a', 'b', 'c', 'd']
var a = ['a', 'b', 'c', 'd', 'e'];
a.splice(2, 1, 'z', 'v'); //Returns ["c"], a is ["a", "b", "z", "v", "d", "e"]