Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSteve Fau
5,622 PointsPushing 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?
2 Answers
rydavim
18,814 PointsMy 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!
Anastasios Poursaitedes
10,491 PointsThe const keyword doesn't allow reassignment, which means that you cannot reassign the array with some other data(assign a string to it for example). That doesn't mean that you can't mutate the elements of the array. You can remove, add new elements and change existing elements in the array. The array's contents remain mutable.
Steve Fau
5,622 PointsSteve Fau
5,622 PointsThanks a lot :)
Yeah I guess one has to differentiate between reassigning and manipulating. I'll also do some more reading :)