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!

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 Basics Storing and Tracking Information with Variables Define Variables with let and const

Farid Lavizadeh
Farid Lavizadeh
12,006 Points

I don't get the expected error when using "let" with the same name twice.

For the following code, I don't get an error on Chrome Console - I just get "15" as value:

let score = 5; score += 10;

let score = 20; console.log(score);

5 Answers

Steven Parker
Steven Parker
228,026 Points

Odd, when I put those lines into the Chrome console simultaneously I get:
ā€ƒ "Uncaught SyntaxError: Identifier 'score' has already been declared at <anonymous>:1:1"

Kim B
Kim B
4,944 Points

I am having the same issue. If I'm in the Console and I say let score = 10; and then later (either in the same line or further down) I either say score = 5 or let score = 5 or whatever, it does not give me an error message.

It will not let me do the same with constā€”I do get an error if I try redeclaring or reassigning. Is this just a peculiarity of working in the Console? I see a few answers suggesting that below, but I don't understand why I can't get it to give me an error with let, and I don't know enough about JS to experiment outside of the console with practical examples lol.

Steven Parker
Steven Parker
228,026 Points

Kim B ā€” yes, this is unique to the console. See the explanation by volhaka below.

For future issues, to reach more students always create a fresh question instead of asking one as an answer or comment.

volhaka
volhaka
9,596 Points

if you put into console together as script you will get an error "Uncaught SyntaxError: Identifier 's' has already been declared" because it works as one script and it is not allowed to declare the same var twice with "let"

let score = 5; 
let score = 20;

if you enter the same 2 declarations individually into console, one by one, it is like 2 different scripts and you will not get any errors

It will show 15 as a result if you write only: let score = 5; score += 10; Try to copy the code as you wrote it here and paste it in console. You will get the error.

Are you assigning the values directly from chrome dev tools? If so you will not get an error, but if you follow this lesson from your code editor will display the same error message as Guil.

ChienTsun Chan
ChienTsun Chan
7,042 Points

I put the same code line as u, but I indeed got the error "Identifier has already been declared".