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 trialFarid Lavizadeh
12,006 PointsI 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
231,275 PointsOdd, when I put those lines into the Chrome console simultaneously I get:
ā "Uncaught SyntaxError: Identifier 'score' has already been declared at <anonymous>:1:1"
volhaka
9,875 Pointsif 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
marialena alevizaki
10,296 PointsIt 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.
jlampstack
23,932 PointsAre 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
7,042 PointsI put the same code line as u, but I indeed got the error "Identifier has already been declared".
Kim B
4,991 PointsKim B
4,991 PointsI 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 sayscore = 5
orlet 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 withlet
, and I don't know enough about JS to experiment outside of the console with practical examples lol.Steven Parker
231,275 PointsSteven Parker
231,275 PointsKim 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.