Copy an array in Javascript by value, not by reference cover image

Copy an array in Javascript by value, not by reference

Sam Ciaramilaro • April 9, 2015

Snippets
var food = ['pizza', 'pasta', 'meatballs'];
var foodAgain = food.slice();

foodAgain.push("lasagna");

console.log(foodAgain); // ['pizza', 'pasta', 'meatballs']
console.log(food); // ['pizza', 'pasta', 'meatballs', 'lasagna']