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 Creating Unchanging Variables With const

Blake Powell
Blake Powell
4,175 Points

What happens to joel kraft

so i get why it keeps the name andrew, but not quite clear on whats happening within the function. because you change it from a variable to a constant, the scope is limited to within the function correct? but what can happen with that new constant now, wouldn't it have to find its way into another variable/constant to be of any use?

a separate question, we may be getting to this later, but with the first variable being changed to a constant i see that it can't be replaced, but can we add to the string with a +=to create a simple list?

2 Answers

Steven Parker
Steven Parker
229,004 Points

After the change, there are two constants.

Originally, the function uses the global name, that's why there is an error when the global name is changed to a constant. But when "const" is put in front of name inside the function, a new constant is created which has the scope limited to within the function. It lasts long enough to use it in the call to console.log.

A constant cannot be changed., that means neither a normal assignment ("=") nor an addition assignment ("+=") can be used on it.

Ana Luiza Barreto Marinho
Ana Luiza Barreto Marinho
2,210 Points

Not really, no... That new constant is locked to that function's scope, that means for all the rest of the program cares, the only variable available is the first const name = Andrew, the other one doesn't exist. That implies that you can still use it inside that function and use the function in the code with no problem what so ever :).

Now that second question: no, it can't be modified in any way... not modifying the whole value or parts of it :).