let javascript
// var has function scope.
// let has block scope.
function func(){
if(true){
var A = 1;
let B = 2;
}
A++; // 2 --> ok, inside function scope
B++; // B is not defined --> not ok, outside of block scope
return A + B; // NaN --> B is not defined
}
4
3
The let statement declares a block-scoped local variable,
optionally initializing it to a value.
let x=1;
Thank you!
3
0
4.25
8
The let statement declares a block scope local variable, optionally initializing it to a value.
let x = 1;
if (x === 1) {
let x = 2;
console.log(x);
// expected output: 2
}
console.log(x);
// expected output: 1
Thank you!
8
0
0
2
var i = "global";
function foo() {
var i = "local"; // Otra variable local solo para esta función
console.log(i); // local
}
foo();
console.log(i); // global
Thank you!
2
0
3.33
3
let: //only available inside the scope it's declared, like in "for" loop,
var: //accessed outside the loop "for"
Thank you!
3
0
Are there any code examples left?
New code examples in category Javascript