Splice() copy confusion

Heyy, so while I was doing the Javascript course, I came across this lesson: Remove Items Using splice() that said

splice() not only modifies the array it’s being called on, but it also returns a new array containing the value of the removed elements:

Code example:

let array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2);


[ 'I', 'am', 'feeling' ]

and then a few lessons later, namely Copy Array Items Using slice()

Rather than modifying an array, slice() copies or extracts a given number of elements to a new array, leaving the array it is called upon untouched.

let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);

todaysWeather would have the value ['snow', 'sleet'] , while weatherConditions would still have ['rain', 'snow', 'sleet', 'hail', 'clear']

and I feel this is contradicting because the first lesson says it modifies the array and the other says it doesn’t or I’m just getting confused for no reason and not able to grasp properly I don’t understand.

Please help a stranger out. Thank you :smile:

those are two methods slice() and splice(), its easy to get confused :stuck_out_tongue_winking_eye:


Oh my God I’m shaking my head rn this is so dumb but thank you soo much @Sylvant

1 Like

Splice and Slice both are Javascript Array functions. The splice () method returns the removed item(s) in an array and slice () method returns the selected element(s) in an array, as a new array object. The splice () method changes the original array and slice () method doesn’t change the original array.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.