HuntExams Academy logo
JavaScriptBeginner#arrays

Difference between slice and splice?

Answer

slice returns a shallow copy of a range and leaves the original untouched. splice mutates the array by removing and/or inserting elements and returns the removed items.

Example

const a = [1,2,3,4];
a.slice(1,3);      // [2,3], a unchanged
a.splice(1,2,'x'); // returns [2,3], a is [1,'x',4]

Related Interview Questions

1
JavaScriptBeginner#arrays

What do find, findIndex, some and every do?

Open
2
JavaScriptBeginner#es6

What is destructuring?

Open
3
JavaScriptBeginner#es6

Difference between spread and rest operators?

Open