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 trial

JavaScript JavaScript Arrays Store Multiple Values in an Array What is an Array?

Question regarding const

Could someone help clear this up for me. In the video Guil uses const to declare the array, am I right in assuming that this works fine because the values stored inside the array are mutated and not replaced, like primitive values would be in a string or number for example?

2 Answers

Daniel Jong
Daniel Jong
11,071 Points

Hi, while using const for arrays you can change the value within the array or using methods like push() to add new items to your array but you cannot reassign a new array for the const array

For example:

const arr1 = [1,2,'hi'];
arr1.push(4);
arr1[2] = 3;
// This is valid

const arr2 = [4,5,6];
arr1 = arr2;
//Reassigning is not valid for const variables

That makes much more sense. Thanks!