let of javascript code example
Example 1: var vs let js
let:
var:
Example 2: 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 3: javascript for of loop
for (variable of iterable) {
statement
}
Example 4: mdn for..of
function* fibonacci() {
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (const n of fibonacci()) {
console.log(n);
if (n >= 1000) {
break;
}
}
Example 5: let javascript
var i = "global";
function foo() {
var i = "local";
console.log(i);
}
foo();
console.log(i);