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);
console.log(array)
Output:
[ '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']
, whileweatherConditions
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