var and let code example
Example 1: javascript var and let
function func(){
if(true){
var A = 1;
let B = 2;
}
A++;
B++;
return A + B;
}
Example 2: var vs let js
let:
var:
Example 3: let in javascript
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);
}
console.log(x);
Example 4: let and var difference
function run() {
var foo = "Foo";
let bar = "Bar";
console.log(foo, bar);
{
let baz = "Bazz";
console.log(baz);
}
console.log(baz);
}
run();