Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Using splice() to Remove Elements

By strategically setting parameters, you can utilize splice() to eliminate elements from an array without leaving any “holes” behind.

Example

const fruits = [“Banana”“Orange”“Apple”“Mango”];
fruits.splice(01);

The first parameter (0) specifies the position where new elements should be inserted (spliced in).

The second parameter (1) specifies the number of elements to be removed.

JavaScript Array toSpliced()

In ES2023, the toSpliced() method was introduced as a safer alternative to splice(), allowing array modification without altering the original array, with no new elements added when subsequent parameters are omitted.

The key difference between toSpliced() and splice() is that toSpliced() creates a new array without altering the original, while splice() modifies the original array.

Example

const months = [“Jan”“Feb”“Mar”“Apr”];
const spliced = months.toSpliced(01);