I ran both examples using var instead of let keyword in my browser console and both outputs where exactly the same as if using let (Burrito and 350). Based on this result, please help me better understand the differences.
I would like to know the difference as well.
So do ı ⊠Whatâs difference?
here is a good explanation:
which goes into more depth, the simplified version is that let
is block scoped:
if (true){
let letVariable = 'hello world';
var varVariable = 'hello world';
console.log(varVariable);
console.log(letVariable);
}
console.log(varVariable);
console.log(letVariable); // syntax error or undefined
this is because the variable declared with let only exist within the if block (between the {}
)
Hi Codecademy learners. This is with respect to the exercise âdifference between var and letâ - the lesson says that we can only reassign variables that were declared using âletâ. But I ran the same example example using âvarâ and it still works. Why is that? What is the little difference that Iâm missing? I found a link to one of the codecademy posts that says let is block-scoped and var is not. So what happens if I need to change a global variable (doesnât that global variable need be declared as var in that case?)
var meal = 'Enchiladas';
console.log(meal); // Output: Enchiladas
meal = 'Burrito';
console.log(meal); // Output: Burrito
just like any piece of software, programming languages get updates. The latest major update of javascript was in 2015 (version 6, known as es6 or emcascript 2015). Before 2015 we then obviously had version 5 (es5)
let
and const
where introduced in es6, so when the lesson says we can only reassign variable declared with let, its comparing with const
, not the older var
(var
is es5, or maybe older)
no, we can declare global variable with let
, then the scope is the same (global). But you need to be aware of the block scope when that comes into play (when variable are declared in a block). Global variable declared with let are not added to the window
object (see the stackoverflow question in my previous answer in this topic)
Var has not become completely useless, but the problem is that when you just start to learn JS, these concepts (like the window object), are difficult to grasp, because you donât have much experience with them. So its okay if you donât comprehend it all yet, that will come with time
Thank you! It helped.
The difference is scoping. var
is scoped to the nearest function block and let
is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.
Also, variables declared with let
are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.
Global:
They are very similar when used like this outside a function block.
let me = 'go'; // globally scoped
var i = 'able'; // globally scoped
However, global variables defined with let
will not be added as properties on the global window
object like those defined with var
.
console.log(window.me); // undefined
console.log(window.i); // 'able'
Function:
They are identical when used like this in a function block.
function ingWithinEstablishedParameters() {
let terOfRecommendation = 'awesome worker!'; //function block scoped
var sityCheerleading = 'go!'; //function block scoped
}
Block:
Here is the difference. let
is only visible in the for()
loop and var
is visible to the whole function.
function allyIlliterate() {
//tuce is *not* visible out here
for( let tuce = 0; tuce < 5; tuce++ ) {
//tuce is only visible in here (and in the for() parentheses)
//and there is a separate tuce variable for each iteration of the loop
}
//tuce is *not* visible out here
}
function byE40() {
//nish *is* visible out here
for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
}
//nish *is* visible out here
}
Redeclaration:
Assuming strict mode, var
will let you re-declare the same variable in the same scope. On the other hand, let
will not:
'use strict';
let me = 'foo';
let me = 'bar'; // SyntaxError: Identifier 'me' has already been declared
'use strict';
var me = 'foo';
var me = 'bar'; // No problem, `me` is replaced.
What is the difference between var
and let
keywords like when to use var
or when to use let
?
var
has function scope, while let
has block scope:
function example():
if (true) {
var functionScope = true;
let blockScope = true;
}
console.log(functionScope);
console.log(blockScope); // this line gives an error
}
the reason why blockScope
gives an error, is because it only exists within the if block. functionScope
is also defined within the if block, but its scope rules are different
you should use let
mostly, given its newer and block scope leads to less scope pollution and bugs. For now, assume this is true, you will learn more about it once you gain more experience.
I havenât really understand what is the difference between var and let. And I am saying that, because even if I create a var or a let, I can change them both later⊠Their values I mean.
have you read some of the answers in this topic? There are plenty of answers explaining the differences
This being the most extensive answer:
I still find it hard to understand the difference.
Again, did you read the explanations in this topic? There are several
Thank you for detailed explanation. However, many of us who did not have computer science background will find hard to follow your terminology used here.
So is var immutable?
Also, if we donât have to use var or let to create a variable, then why or when should we use let or var?
Scope rules. For example:
x = 3;
const example = () => {
x = 5;
}
example()
console.log(x);
vs:
let x = 3;
const example = () => {
let x = 5;
}
example()
console.log(x);
now you donât overwrite global variable by accident (which could otherwise lead to bugs)
preferable, use let
, which is block scoped:
for (var i = 0; i < 5; i++){
console.log(i);
}
console.log(i);
vs:
for (let i = 0; i < 5; i++){
console.log(i);
}
console.log(i); // gives error
With let
, the variable doesnât pollute the scope and only exists when it has to
I have a question about the âletâ keyword. what is the difference between that and just reassigning the variable? what would be the difference between this code:
var ranNum = 5;
console.log(ranNum);
ranNum = 10;
console.log(ranNum);
and this code?:
let ranNum = 5;
console.log(ranNum);
ranNum = 10;
console.log(ranNum);
In this case? Not much, but you have to understand the difference between let
and var
.
let is block scoped:
if (true) {
let x = 3;
}
console.log(x); // error
while var
is functional scoped:
if (true) {
var x = 3;
}
console.log(x); //logs 3