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 Overview

Brendan Moran
Brendan Moran
14,052 Points

PSA: const and let cannot be redeclared.

I don't think this was mentioned in the workshop, but I stumbled upon it in some further reading and it seemed very helpful and worth mentioning: whereas var can be redeclared, neither const nor let can be redeclared.

What was mentioned in this workshop was concerning reassignment: that const cannot be reassigned, whereas let can. Reassignment being:

//this works
let sheepSay = "baa";
sheepSay = "moo"

//this doesn't
const sheepSay = "baa";
sheepSay = "moo";

However, neither one can be redeclared/reinitialized, and this is another aspect of what makes them good bug-stoppers and different from var. I think Andrew mentioned not being able to redeclare a const, but I'm not sure if he mentioned it with let.

//this won't work, thankfully:
let sheepSay = "baa";
let sheepSay = "moo";

//...syntax error will be thrown and sheep will still say "baa".

const sheepSay = "baa";
const sheepSay = "moo"; //will also throw an error giving the same reason as let: variable with this name already exists.

var sheepSay = "baa";
var sheepSay = "moo"; //unfortunately, this works. Sheep now say "moo".

3 Answers

let is very different from an const. The value of a constants once assigned are final but a let statements can change their values.

What you can't do is reinitialize it within the same scope. The other difference between a let and a var is that vars are instantiated before any JavaScript code executes which is strange in most programing languages. This means that the following code is valid in JavaScript.

value = 5; var value;

A let statement can not be used in the same way. You must instantiate the variable with a let statement before being able to use it.

andren
andren
28,558 Points

I don't think Brendan Moran is confused about the difference between let and const. Brendan is just pointing out that the video does not mention the fact that let and const cannot be redeclared. Which is something that is worth noting since it is a different behavior than you have with var variables.

It is more of a PSA than a question.

Brendan Moran
Brendan Moran
14,052 Points

andren is correct, this is a PSA and I've edited the question a bit to clarify that. Obviously, const and let are very different, and both are very different from var. I am pointing out one aspect of how they are both different from var that was not mentioned in the workshop. I thought this aspect of const and let (can't reinitialize/redeclare in same scope) was very important and worth mentioning.

Thanks Brendan, this is a helpful PSA!

Thank you for clarifying! This was very helpful.