.concat returns a new array containing the old and new data, where as
.push modifies the array used to call it.
let array1 = [1, 2, 3]; let array2 = [1, 2, 3]; let pushResult = array1.push(4); let concatResult = array2.concat(4); console.log(pushResult); // logs "4". Wonderful. console.log(concatResult); // logs "[1, 2, 3, 4]". Actually wonderful.
You should opt for immutability here. Using
concat will keep the original intact and give you a new array. Consider a situation where you are referencing a nested array in an object. If you use
.push, you have now altered the object. This may have unintended side effects.
Speaking from experience, as I just spent half an tracking down why my variable was suddenly different. Somewhere in my code, a
.push was altering my data.
constwould have prevented this! It never would have worked if you used
No, wrong, incorrect. In ES6, when using
const with an array or object, it just ensures that the reference doesn't change. The data in the object/array can change all it wants.
Be nice to others. Use