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 Objects Loop Through Objects Use `for in` to Loop Through an Object's Properties

Gurveer Singh
Gurveer Singh
11,367 Points

const confusion = 'what it is actually for?'

Firstly, const was used because no one can change its value after. But as we keep goin in course in the section of array, objects, loop etc. its value can be changed or added. Why is this happening? and why we are using const instead of let if its features are same in here?

Please answer if anyone can

1 Answer

Steven Parker
Steven Parker
231,269 Points

The important thing to keep in mind about a const is you cannot change its value while it exists. This is an important distinction, because in the context of a loop, it is re-created for each loop iteration. This allows it to be given a new value for each cycle, but it still cannot be changed within the loop code.

Also, the unchangeable aspect applies only to the variable itself, so a const array cannot be reassigned, but the values of its elements can be changed. Similarly a const object cannot be reassigned, but its properties can be changed.