JavaScript JavaScript Arrays Store Multiple Values in an Array Add Elements to an Array

Steve Fau
Steve Fau
3,523 Points

Pushing to an array declared as a const?

For example:

const items = [1, 'two'] items.push('three', 4) console.log(items.length) // 4

^ shouldn't that throw an error?

1 Answer

rydavim
MOD
rydavim
Treehouse Moderator 18,640 Points

My understanding is that const in this example means the variable can't be reassigned not that you can't manipulate it. However, depending on the application it may or may not be good form - functional programming would likely discourage mutating the array.

Let me know if that doesn't answer your question and I can look into it more deeply. Happy coding!

Steve Fau
Steve Fau
3,523 Points

Thanks a lot :)

Yeah I guess one has to differentiate between reassigning and manipulating. I'll also do some more reading :)