Explore differences between var and let

I’m going through the exercises for practice, and it says that if I use let, I won’t be able to overwrite the variable. But, when I open up a different tab and try it in the console, it lets me. Am I missing something here?

It doesn’t say you cannot overwrite/change the value stored in the variable. It says that you cannot re-declare the variable in the same scope.

So unlike var, when you use let, a variable with the same name can only be declared once.

You can review what ‘declaring’ means here:

Could you give me an example of this?

This is not permitted

let camper = "James";
let camper = "David";

because the variable camper is declared twice.

However, this is permitted

let camper = "James";
camper = "David";

I understand that, but my question is, When i put that exact code into the console on a browser. it just gives me undefined and then i can console.log() it and it gives me the “David”
Thank you btw.

Which ‘exact code’? The first or the second?

let camper = “James”;
// then i hit enter to get undefined
let camper = “David”;
//then i hit enter to get undefined
// result is david with no errors

Then your browser is choosing to violate the Javascript standard. Sometimes browsers choose to do that, but you should never write code that depends upon the browser choosing to break the rules.

You can also get strange scoping rules in certain special debugging modes.

1 Like

Thank You for clarifying.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.