ความแตกต่างพื้นฐานระหว่าง Slice และ Splice คือ
splice() เปลี่ยน Original Array ที่ถูกเรียก และคืนค่า Removed Item(s) ใน Array เป็น New Array Object
slice() ไม่เปลี่ยน Original Array และคืนค่า Array ที่ Slice ออกมา
ตัวอย่าง
// splice changes the array let arr = [1, 2, 3, 4, 5]; console.log(array.splice(2)); //slice doesn't change original one let arr2 = [1, 2, 3, 4, 5]; console.log(array2.slice(2)); console.log("\n After Changing the arrays"); console.log(array); console.log(array2);
ผลลัพธ์
[ 3, 4, 5 ] [ 3, 4, 5 ]
After Changing the arrays
[ [ 1, 2 ] [ 1, 2, 3, 4, 5 ]