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 :: jquery vdn 
Javascript :: how to check what browser you are using javascript 
Javascript :: for each python json 
Javascript :: set data attribute with a string jquery 
Javascript :: padend method in javascript 
Javascript :: iterate formData 
Javascript :: firebase storage javascript delete document 
Javascript :: remove the items in array which are present in another javascript 
Javascript :: iterate through list js 
Javascript :: js one line if 
Javascript :: how to go to next page on button click js 
Javascript :: get pods running on a node 
Javascript :: string to int js 
Javascript :: javascript delete first character in string 
Javascript :: dynamically adding marker react native mapbox 
Javascript :: javascript transpose array 
Javascript :: react native check os 
Javascript :: expo image picker 
Javascript :: print hello world in javascript 
Javascript :: async await catch error 
Javascript :: jquery reset form fields 
Javascript :: convert milliseconds to minutes and seconds javascript 
Javascript :: how to send static file in express 
Javascript :: get random element from array js 
Javascript :: cypress have attribute 
Javascript :: array contains multiple js 
Javascript :: js get day month year 
Javascript :: remove appended element jquery 
Javascript :: javascript pick multiple random from array 
Javascript :: map to array javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =