Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Let vs var
Message
General information
Forum:
Javascript
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
01654397
Message ID:
01654398
Views:
72
This message has been marked as a message which has helped to the initial question of the thread.
Please look into MDN around it:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

"let" declares a block scope local variable but "var" defines a variable globally or locally to an entire function regardless of the block scope.
The scope of "var" is the enclosing function.

function varTest() {
var x = 1;
if (true) {
var x = 2; // same variable!
console.log(x); // 2
}
console.log(x); // 2
}

function letTest() {
let x = 1;
if (true) {
let x = 2; // different variable
console.log(x); // 2
}
console.log(x); // 1
}

"let" doesn't create a property on the global object like "var" when it comes to main level of the program.
var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined

Variable Hoisting doesn't apply to "let" variables and then "let" doesn't move to the top of the current execution context like "var".
function do_something() {
console.log(bar); // undefined
console.log(foo); // ReferenceError
var bar = 1;
let foo = 2;
}

Then changing "let" to "var" or vise versa may affect the code, Hope that helps.
Previous
Reply
Map
View

Click here to load this message in the networking platform