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

Oh, thank you, my bad

That is okay. But as a developer, google is friend. Being able to find the information you are looking for/that can help you, is a vital skill :slight_smile:

2 Likes

Thank you so much, I checked all those answers, yours are the simplest and most effective.

This the best example.

see its pretty easy
var and let have different scope
you can say let is block type,
there is good example
{
for(var i=0;I<6;i++){
setTimeinterval({
console.log
},1000)
}
}
// here answer you get is
7
7
7
7
7
7
7
why? because its var here is globar scope.
{
for(let i=0;I<6;i++){
setTimeinterval({
console.log
},1000)
}
}
but now if you check now
let is local variable
so answer will be 1 to 6 as we expected.

see that’s interesting. I’m glad I read through here at the end of each lesson. it completely redefined the lesson for me. I’m guessing the lesson was based on older version of es6

Hey is it ok if i dont understand this. i am new to JS anyway

So, I read it again and again and I got more confused then I was.

var is not supposed to be used anymore, right? We learn it just to understand old codes, right? If I create something I’m not supposed to use var and use let, right?

and what are scopes and a what is block-scope?

Learning about the different scopes will make you a better developer. And there might be rare cases (legacy code for example) where var used or needed.

but preferable, use let indeed.

So what is the difference between these;

let name = “Joe”;

name = “Joe”;

var name = “Joe”;

You do this:
let name = “Joe”;
name = “Bob”;
So why do we even need let, var, or const?

let is block-scoped, var is function scoped. Also how this variables are handled by the window object are slightly different.

preferable, use let, its the newest and block-scoped.

we need these keywords for various reason, to ensure variable don’t pollute outside there scope. Writing good and clean code means you need the right variable scopes

On the last section of your response in the Redeclaration section, that is really confusing me. I thought “let” was able to be redefined and var wasn’t able to be redefined. What is strict mode anyways? Is strict mode usually in place by default when running Javascript?

There is good documentation on strict mode:

Strict mode - JavaScript | MDN

both var and let can be re-defined/re-assigned, const can not be re-assigned

var and let do have different scope

1 Like

Thank you stetim! I should’ve read more before asking. I found out after I looked through some MDN articles on it. Appreciate your response!

1 Like

Thanks, these jewels are well appreciated.

That means var variables have global scope and let variables have block scope

var has functional scope:

const abc = () => {
   var example = 'hello world'
}

abc()
console.log(example); // error
1 Like

var age = 20;
if(age > 18){
age = age +2;
console.log(I am ${age} years old);
}
console.log(age); // 22

well what should i call this here?

global variable are global, both for let and var

I only wanted to demonstrate that var isn’t always global (like when defined in the body of a function)

1 Like
  • Do not use var. It has scoping differences which can lead to unexpected results and it can be re-declared which can also lead to bugs

  • Use let if you have a primitive variable that is not constant (i.e you will change its value)

  • Use const if you have a primitive variable that you do not want the value to change (a constant) or an object (you can still change the data within an object if it is a const)