js .splice() code example

Example 1: splice javascritp

let colors = ['red', 'blue', 'green'];

let index_element_to_be_delete = colors.indexOf('green');

colors.splice(index_element_to_be_delete); 

//Colors now: ['red', 'blue']

Example 2: splice javascript

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

months.splice(0, 1);
// removes 1 element at index 0
console.log(months);
// expected output: Array ["Feb", "March", "April", "May"]

Example 3: splice method js

let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
//insert new element into array at index 2
let removed = myFish.splice(2, 0, 'drum')

// myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"] 
// removed is [], no elements removed

Example 4: splice in javascript

Splice and Slice both are Javascript Array functions. Splice vs Slice. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object. The splice() method changes the original array and slice() method doesn't change the original array.27-Jan-2018

Example 5: what is splice method in javascript

Check Out my CODE-PEN for this Solution 

https://codepen.io/siddhyaOP/pen/eYgRGOe
https://codepen.io/siddhyaOP/pen/eYgRGOe
https://codepen.io/siddhyaOP/pen/eYgRGOe

Tags:

Css Example