English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript Statements and Variable Declarations
letThe statement declares a block {} local variable scope and can optionally initialize it with a value.
let allows you to declare variables whose scope is limited to the block, statement, or expression where the variable is used.
This isvarThe keyword is different, this keyword defines a variable in the global scope or in the local function scope regardless of the block scope.
You can find more information in ourIn the JavaScript scope tutorialLearn more about variable scope.
let identifier = value;
let y = "world";Test and see‹/›
The numbers in the table specify the first browser version that fully supports the let statement:
Statement | |||||
let | 49 | 44 | 17 | 10 | 12 |
Parameter | Description |
---|---|
identifier | Specify the name of the variable. It can be any valid identifier. Variable names can contain letters, numbers, underscores, and dollar signs.
|
value | The initial value of a variable. It can be any valid expression. Default valueundefined |
JavaScript Version: | ECMAScript 1 |
---|
When used within the block,letLimit the scope of the variable to within this block:
var a = 1; var b = 2; if (a == 1) { var a = 11; // a is in global scope let b = 22; // The scope of b is within the if code block document.writeln(a); // 11 document.writeln(b); // 22 } document.writeln(a); // 11 document.writeln(b); // 2Test and see‹/›
JavaScript Tutorial:JavaScript Variables
JavaScript Tutorial:JavaScript Scope