What are the differences between `var` and `let`? Which should I use?

Do not use var is not something I agree with, sure, 95% of the situations let will very likely be better, but you should understand the var scope rule and what this could be problematic


I cant think of scenario where i’d use let instead of just using var every time.

None of the replies in this topic convince you that let is the better solution? var might introduce scope bugs, and leak the variable outside its intended scope, or unnecessary pollute the namespace

I will be honest that I do not understand the difference either. I ran the code for the exercise and a version I created using var and got the same result as well:

let changeMe = true;

changeMe = false;


var makeMe = true;

makeMe = false;


If anybody found the answer and can explain it in lamented terms, I would be very recognising. Thank you! :slight_smile:

Disclaimer: my answer is going to be very subjective here.

Having clarified that, I don’t think at any point in your career you need to know absolutely every tiny detail that there is about decisions of ECMAScript and everything that there is out there to know. Sometimes, the best way to go by, again in my personal opinion, is by certain principles.

How to apply that to this case? Well, ECMAScript decided to incorporate into ES5 the const and let to in most cases/scenarios replace the var keyword. Cool! Then let’s use them instead, because, to be honest, these guys that make those standards are waaay smarter than I will ever be. :slight_smile:

I take it that the difference between const and let you have it clear. So, if one day, you find a situation where const and let seem not to be enough or to give a certain issue, it might be helpful to consider trying with var.

If, instead of writing, you read var, then be aware that there might be little details where it works differently. Well, in that case, you could always copy the code and change it for let to see if the issue you have is related to var.

I think that would make things simpler for you to keep on track learning the really important things (how to solve problems with coding) rather than the oftentimes unimportant nuances.

Again, that’s very subjective, so take it or leave it, and if you take it, with a grain of salt, because another person might have a different opinion. But hope it helps you to avoid necessary distractions so you get faster to the satisfaction of doing cool things with coding! :muscle:

1 Like

There are good answers already in this topic:

1 Like

If that’s the case, why would some rather use let instead of always using var? Is it because let uses up less “memory space”, because its limited to blocks?

I’m brand new to other forms of variables other than var, because I’ve come from KA, where they only use var.

yes, block scope is in most cases preferable. It keeps your code much readable and understandable

In programming things are seldom absolute, you might still want to use var in certain cases (how var and let differ when it comes to the window object)

you should always try to use the best solution, not use x or y because you can always use it.

Okay, thank you for clearing that up for me.

what is the point of declaring a variable using var, let or const? If i don’t use any of those and just say meal = true; it still outputs true even though it isn’t declared.

Surely this has been covered in this topic? not using a keyword will always make the variable global.

there is a difference between working code and good code

this helped me a lot

Hi, my name is Tomas, and Im on same way, Im beginer web developer.
I was try make any code for more understanding and for answer your question, I hope that will make sence and U will quicly understand.

var testing1 = 5;
let testing2 = 3;
if (true) {
console.log(testing1) //work easly
console.log(testing2); // work easly

let test3 = ‘my let test in local scope’;
var test4 = ‘my var test in local score’;
var test5 = test3 + ’ was reasign’;
console.log(test3); //it give you error because let is in local scope, and the console.log can NOT read that from inside, like suck that from the score.
console.log(test4); //that will work because its declated like var property, and var is local and global scope(easy for u .log)
console.log(test5);//here I was the let reasign to var, and then the log can take that from the local scope up and read.

I was watching couple of tutorials on youtube and learn alot before Im here, they many time say, code is reading from buttom to top, and if the log( ) can NOT find the property it start searching back in the local scope and take it up.

When I first read about ‘objects’ I imagined them as actual ‘objects’… like a car or a house.

When I read about ‘scope’, I thought about the difference between ALL cars and MY car.

‘ALL cars’ is a subject/object that can be discussed… but MY car/house are very specific subjects/objects that I own and control.

No one else should be using MY car/house without my explicit permission.

If I put MY phone in MY car, drive MY car across town, leave it in a parking lot for a week, then drive it back home, I expect to find MY phone in MY car.

LET affects MY stuff that I put into MY car/house.

VAR can potentially affect anyone’s stuff, even if they put it into my car/house. I can not stop you from disconnecting cell phone service to YOUR phone that you left in MY house.

If MY car catches on fire and MY phone and MY wallet burn up in MY car… no one else really cares.

But, if ALL cars suddenly stop working… EVERYONE is affected.

I am sure that someone will be able to rationalize that I am wrong (this is the internet after all) but, that is how I think of SCOPE.

the difference is simple
you can set multiple same variable using var and it cause error but you can’t do that with let
you can
var apple
var apple
var apple
but you can’t
let apple
let apple
because console give you error you can’t declare same variable using let.

As much as I know, it is not different between let and var. the only difference is that the first developer used var and then let created, and they used to let it is because more reliable and safe to use.

Why would we create another keyword if there is no difference?

more reliable and safer how?

Pensando assim, o ‘let’ deve ser usado para uma variável de bloco e ‘var’ para variáveis do todo. Será?

No, ideally always use let to ensure variable does not leak outside there scope. Unless you have a good reason to use var

1 Like

I got it! Thank you!