Difference between array.slice().push() and array.concat()

I was working on a problem in the Redux section and was unsure why my solution was unable to append a new todo item to a copy of the original array.
I noticed that example1 returns an integer, while example2 returns the desired array. Why is this?
My frame of thinking for example1 was that I am making a copy of a new array with slice(), and then pushing a string to the end of the copied array.

Thank you

`const sampleArr = [‘arg’, ‘hoy’, ‘matey’, ‘pie’, ‘tings’];

const exmaple1 = sampleArr.slice().push(‘example one’);
const example2 = sampleArr.concat(‘example two’);

console.log(exmaple1);
console.log(example2);`

Array’s push method adds the element in-place at the end of array and returns the new length of array. concat returns completely new array.

1 Like

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