Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Let vs var
Message
Information générale
Forum:
Javascript
Catégorie:
Codage, syntaxe et commandes
Titre:
Divers
Thread ID:
01654397
Message ID:
01654398
Vues:
73
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.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform