DEV Community

bomoniyi
bomoniyi

Posted on

Using array spreads to prevent array overriding

const lunchMenuIdeas = ['Harvest Salad', 'Southern Fried Chicken'];

const allMenuIdeas = [...lunchMenuIdeas];

allMenuIdeas.push('Club Sandwich');

console.log(allMenuIdeas);
Enter fullscreen mode Exit fullscreen mode

using square brackets and ... prevents the lunchMenuIdeas array from being overwritten by allMenuIdeas as arrays are passes as references

Top comments (0)