English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript let statement

 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.

Syntax:

let identifier = value;
let y = "world";
Test and see‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the let statement:

Statement
let4944171012

Parameter value

ParameterDescription
identifierSpecify the name of the variable. It can be any valid identifier.
Variable names can contain letters, numbers, underscores, and dollar signs.
  • Variable names must start with a letter

  • Variable names can also start with $ and _

  • Variable names are case-sensitive (city and City are different variables)

  • Reserved words cannot be used as variable names

valueThe initial value of a variable. It can be any valid expression. Default valueundefined

Technical Details

JavaScript Version:ECMAScript 1

More examples

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);  // 2
Test and see‹/›

Also see

JavaScript Tutorial:JavaScript Variables

JavaScript Tutorial:JavaScript Scope

 JavaScript Statements and Variable Declarations