Invalid assignment to const 'food'

When I try to do the push and unshift examples that you use in this part:

food = [...food, 'Coffee'];

food = ['Coconut', ...food];

food = ['Coconut', ...food, 'Coffee'];


I get the message TypeError {"invalid assignment to const 'food'"} in jconsole.com.


Is this because the array 'food' is previously defined as const and therefore not assignable?

Don't you need to give the array a new name (e.g. food1 = ['Coconut', ...food, 'Coffee'];)?


Link to content: Learn to Code with JavaScript - Section 5

This would be correct. You cannot reassign a constant. You can however change the elements of a constant array, and you can add or remove elements from the array; the array being constant means it constantly refers to that array, but not the contents thereof.

food.push("Coffee"); would be a legal way to add Coffee to the array.