JavaScript let and const
ECMAScript 2015(ECMAScript 6)
ES2015 (ES6) added two important JavaScript keywords: let and const。
A variable declared with let is only valid within the code block in which the let command appears.
const declares a read-only constant; once declared, its value cannot be changed.
Before ES6, JavaScript had only two kinds of scope: Global Variables The difference between local variables inside a function。
Global Variables
Variables declared outside a function have global scope:
Example
var
carName
=
"
Volvo
"
;
//
The carName variable can be used here
function
myFunction
(
)
{
//
The carName variable can also be used here
}
Try it »
Global variables can be accessed anywhere in a JavaScript program.
local variables
Variables declared inside a function have local scope (within the function):
Example
//
The carName variable cannot be used here
function
myFunction
(
)
{
var
carName
=
"
Volvo
"
;
//
The carName variable can be used here
}
//
The carName variable cannot be used here
Try it »
A variable declared with var inside a function can only be accessed inside the function; if var is not used, it becomes a global variable.
JavaScript Block Scope
Variables declared with the var keyword do not have block scope; they can still be accessed outside {}.
{
var x = 2;
}
// Here you can use the x variable
Before ES6, there was no concept of block scope.
ES6 can use the let keyword to achieve block scope.
A variable declared with let is valid only within the code block {} where the let command appears; outside of {} it, it cannot be accessed.
{
let x = 2;
}
// x variable cannot be used here
Redeclaring Variables
Redeclaring a variable with the var keyword can cause problems.
Redeclaring a variable in a block also redeclares the variable outside the block:
Example
var
x
=
10
;
//
Here x is output as 10
{
var
x
=
2
;
//
Here x is output as 2
}
//
Here x is output as 2
Try it »
The let keyword solves this problem because it is only valid within the code block {} where the let command appears.
Example
var
x
=
10
;
//
Here x is output as 10
{
let
x
=
2
;
//
Here x is output as 2
}
//
Here x is output as 10
Try it »
Browser support
Internet Explorer 11 and earlier browsers do not support the let keyword.
The table below lists the minimum browser versions that support the let keyword.
| Chrome 49 | IE / Edge 12 | Firefox 44 | Safari 11 | Opera 36 |
| Mar, 2016 | Jul, 2015 | Jan, 2015 | Sep, 2017 | Mar, 2016 |
Loop Scope
Using the var keyword:
Example
var
i
=
5
;
for
(
var
i
=
0
;
i
<
10
;
i
++
)
{
//
Some code...
}
//
Here i is output as 10
Try it »
Using the let keyword:
Example
var
i
=
5
;
for
(
let
i
=
0
;
i
<
10
;
i
++
)
{
//
Some code...
}
//
Here i is output as 5
Try it »
In the first example, the var keyword was used; the variable it declares is global, both inside and outside the loop.
In the second example, the let keyword was used; the variable it declares is scoped only inside the loop, and variables outside the loop are unaffected.
local variables
Using the var and let keyword to declare a variable inside a function is similar.
Their scope is both local:
// 使用 var
function myFunction() {
var carName = "Volvo"; // 局部作用域
}
// 使用 let
function myFunction() {
let carName = "Volvo"; // 局部作用域
}
Global Variables
Using the var and let keyword to declare a variable outside a function or code block is also similar.
Their scope is both global:
// 使用 var
var x = 2; // 全局作用域
// 使用 let
let x = 2; // 全局作用域
Using Global Variables in HTML Code
In JavaScript, global scope refers to the JavaScript environment.
In HTML, global scope refers to the window object.
Use var Global scope variables declared with the keyword belong to the window object:
Example
var
carName
=
"
Volvo
"
;
//
The variable can be accessed using window.carName
Try it »
Use let Global scope variables declared with the keyword do not belong to the window object:
Example
let
carName
=
"
Volvo
"
;
//
The variable cannot be accessed using window.carName
Try it »
Resetting Variables
Use var Variables declared with the keyword can be modified anywhere:
Example
var
x
=
2
;
//
x is 2
var
x
=
3
;
//
Now x is 3
Try it »
In the same scope or block scope, the let keyword cannot be used to reset var a variable declared with the keyword:
var x = 2; // 合法
let x = 3; // 不合法
{
var x = 4; // 合法
let x = 5 // 不合法
}
In the same scope or block scope, the let keyword cannot be used to reset let a variable declared with the keyword:
let x = 2; // 合法
let x = 3; // 不合法
{
let x = 4; // 合法
let x = 5; // 不合法
}
In the same scope or block scope, the var keyword cannot be used to reset let a variable declared with the keyword:
let x = 2; // 合法
var x = 3; // 不合法
{
let x = 4; // 合法
var x = 5; // 不合法
}
let The keyword can redeclare and assign a variable in a different scope or block scope:
let x = 2; // 合法
{
let x = 3; // 合法
}
{
let x = 4; // 合法
}
Hoisting
In JavaScript, variables defined with the var keyword can be declared after they are used, that is, a variable can be used before it is declared (JavaScript Hoisting)。
Example
//
The carName variable can be used here
var
carName
;
Try it »
Variables defined with the let keyword cannot be declared after they are used; they must be declared before use.
// 在这里不可以使用 carName 变量
let carName;
const Keyword
const is used to declare one or more constants; they must be initialized at declaration, and their values cannot be changed after initialization:
Example
const
PI
=
3.141592653589793
;
PI
=
3.14
;
//
Error
PI
=
PI
10
;
//
Error
Try it »
constDefining a constant is similar to defininglet a variable with the keyword:
- Both have block scope
- Neither can have the same name as other variables or functions in its scope
There are two more differences between them:
constA constant declared with the keyword must be initialized, whileleta variable declared with the keyword does not need to be.- The value of a constant defined with const cannot be changed by reassignment, nor can it be redeclared. A variable defined with let can have its value changed.
var x = 10;
// x is output as 10
{
const x = 2;
// x is output as 2
}
// x is output as 10
A constant declared with const must be initialized:
// 错误写法
const PI;
PI = 3.14159265359;
// Correct way
const PI = 3.14159265359;
Not a True Constant
The nature of const: a variable defined with const is not a true constant and is not immutable; it defines a constant reference to a value. Objects or arrays defined with const are actually mutable. The following code will not throw an error:
Example
//
Create a constant object
const
car
=
{
type
:
"
Fiat
"
,
model
:
"
500
"
,
color
:
"
white
"
}
;
//
Modify a property:
car
.
color
=
"
red
"
;
//
Add a property
car
.
owner
=
"
Johnson
"
;
Try it »
But we cannot reassign a constant object:
Example
const
car
=
{
type
:
"
Fiat
"
,
model
:
"
500
"
,
color
:
"
white
"
}
;
car
=
{
type
:
"
Volvo
"
,
model
:
"
EX60
"
,
color
:
"
red
"
}
;
//
Error
Try it »
The following example modifies a constant array:
Example
//
Create a constant array
const
cars
=
[
"
Saab
"
,
"
Volvo
"
,
"
BMW
"
]
;
//
: the array index value starts from 0.
cars
[
0
]
=
"
Toyota
"
;
//
Adding elements
cars
.
push
(
"
Audi
"
)
;
Try it »
But we cannot reassign a constant array:
Example
const
cars
=
[
"
Saab
"
,
"
Volvo
"
,
"
BMW
"
]
;
cars
=
[
"
Toyota
"
,
"
Volvo
"
,
"
Audi
"
]
;
//
Error
Try it »
Browser support
Internet Explorer 10 and earlier browsers do not support the const keyword.
The table below lists the minimum browser versions that support the const keyword.
| Chrome 49 | IE / Edge 11 | Firefox 36 | Safari 10 | Opera 36 |
| Mar, 2016 | Oct, 2013 | Feb, 2015 | Sep, 2016 | Mar, 2016 |
Resetting Variables
Use var Variables declared with the keyword can be modified anywhere:
Example
var
x
=
2
;
//
Legal
var
x
=
3
;
//
Legal
x
=
4
;
//
Legal
In the same scope or block scope, the const keyword cannot be used to reset var and leta variable declared with the keyword:
var x = 2; // 合法
const x = 2; // 不合法
{
let x = 2; // 合法
const x = 2; // 不合法
}
In the same scope or block scope, the const keyword cannot be used to reset const a variable declared with the keyword:
const x = 2; // 合法
const x = 3; // 不合法
x = 3; // 不合法
var x = 3; // 不合法
let x = 3; // 不合法
{
const x = 2; // 合法
const x = 3; // 不合法
x = 3; // 不合法
var x = 3; // 不合法
let x = 3; // 不合法
}
const The keyword can redeclare and assign a variable in a different scope or block scope:
const x = 2; // 合法
{
const x = 3; // 合法
}
{
const x = 4; // 合法
}
Hoisting
Variables defined with the JavaScript var keyword can be declared after they are used, that is, a variable can be used before it is declared (JavaScript Hoisting)。
Example
carName
=
"
Volvo
"
;
//
The carName variable can be used here
var
carName
;
Try it »
Variables defined with the const keyword cannot be declared after they are used; they must be declared before use.
carName = "Volvo"; // 在这里不可以使用 carName 变量
const carName = "Volvo";