let

Defination

The let statement declares a block scope local variable, optionally initializing it to a value.

Syntax

1
let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];
Copied!

Examples

1
function varTest() {
2
var x = 1;
3
if (true) {
4
var x = 2; // same variable!
5
console.log(x); // 2
6
}
7
console.log(x); // 2
8
}
9
10
function letTest() {
11
let x = 1;
12
if (true) {
13
let x = 2; // different variable
14
console.log(x); // 2
15
}
16
console.log(x); // 1
17
}
Copied!

Usage

1
function Title(props) {
2
let content = props.content
3
return (
4
<h1>{content}</h1>
5
)
6
}
Copied!

References