Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

difference between slice and splice

splice() method changes the original array. 
slice() method doesn't change the original array.

both returned selected elements.
Comment

javascript slice vs splice

var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
 
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.
 
var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
// shows [8, 9]
 
console.log(array2.slice(-2,4));
// shows [9]
 
console.log(array2.slice(-3,-1));
// shows [8, 9]
 
console.log(array2);
// shows [6, 7, 8, 9, 0]
Comment

javascript slice vs splice

var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
 
console.log(array);
// shows [1, 2], original array altered.
 
var array2=[6,7,8,9,0];
console.log(array2.splice(2,1));
// shows [8]
 
console.log(array2.splice(2,0));
//shows [] , as no item(s) removed.
 
console.log(array2);
// shows [6,7,9,0]
 
var array3=[11,12,13,14,15];
console.log(array3.splice(2,1,"Hello","World"));
// shows [13]
 
console.log(array3);
// shows [11, 12, "Hello", "World", 14, 15]
 
           -5 -4 -3 -2 -1
            |  |  |  |  |
var array4=[16,17,18,19,20];
             |  |  |  |  |
             0  1  2  3  4
 
console.log(array4.splice(-2,1,"me"));
// shows  [19]
 
console.log(array4);
// shows [16, 17, 18, "me", 20]
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to convert jsonobject to string in java 
Javascript :: react navigation history clear 
Javascript :: how to clear node modules folder from your computer 
Javascript :: js find value in array 
Javascript :: jquery get all input name and values and submit 
Javascript :: javascript DOM query selector 
Javascript :: console shortcut chrome 
Javascript :: regex must match exactly 
Javascript :: how to delete an element of an array in javascript 
Javascript :: javascript vector 
Javascript :: js base64 encoding 
Javascript :: vuetify autocomplete get input value 
Javascript :: fahrenheit to celsius in javascript 
Javascript :: radio button getelementsbyname 
Javascript :: match the pattern in the input with javascript 
Javascript :: discord js lockdown command 
Javascript :: How to send form data from react to express 
Javascript :: how to iterate array in javascript 
Javascript :: moment get timestamp 
Javascript :: find property in nested object 
Javascript :: grayscale image in canvas 
Javascript :: check which is dubicate in object of array 
Javascript :: how to find duplicate values in an array javascript 
Javascript :: beautify console log result 
Javascript :: npm react dropdown 
Javascript :: angular 8 filter array of objects by property 
Javascript :: alert, react native alert 
Javascript :: siwtch case javascript 
Javascript :: macos start simulator from cli 
Javascript :: iso 8601 date to Js date 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =