JavaScript Array slice() and splice() method Explained⚡️

JavaScript Array slice() and splice() method Explained⚡️

·

1 min read

🔼 slice() method

☑️ The slice() method returns a new array containing a portion of the array selected from start to end ( end not included)

☑️ The slice() method does not change the original array.

const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);

**Array.slice() returns selected array elements as a new array:

Orange,Lemon**

🔼 splice() method

☑️ The splice() method adds and/or removes array elements.

☑️ The splice() method changes the original array

🎯Syntax

array.splice(index, deleteCount, item1, ....., itemN)

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"]**

That's all for now Guys

Thank you for reading this.I hope will helpfull