let Declaration
JS · Variables & Constantssyntax
let variableName = value;example
let score = 0;
score = 10;
console.log(score);output
10Note Block-scoped. Cannot be re-declared in the same scope. Preferred for values that change.
2 snippets in JavaScript
Also written as var vs let
let variableName = value;let score = 0;
score = 10;
console.log(score);10Note Block-scoped. Cannot be re-declared in the same scope. Preferred for values that change.
var variableName = value;function demo() {
if (true) {
var leaked = "visible outside block";
}
console.log(leaked);
}
demo();"visible outside block"Note Function-scoped, not block-scoped. Hoisted to the top of the function. Avoid in modern code -- use let or const instead.