JavaScript let
Example
Create a variable called carName and assign the value "Volvo" to it:
More examples below.
Description
The let
statement declares a variable.
Variables are containers for storing information.
Creating a variable in JavaScript is called "declaring" a variable:
After the declaration, the variable is empty (it has no value).
To assign a value to the variable, use the equal sign:
You can also assign a value to the variable when you declare it:
Note
A variable declared without a value have the value undefined
.
See Also:
JavaScript Reference: JavaScript var
JavaScript Reference: JavaScript const
Tutorials:
JavaScript Tutorial: JavaScript Variables
JavaScript Tutorial: JavaScript let
JavaScript Tutorial: JavaScript const
JavaScript Tutorial: JavaScript Scope
Syntax
Parameters
The name of the variable.
Variable names must follow these rules:
Must begin with a letter, or ,ドル or _
Names are case sensitive (y and Y are different)
Reserved JavaScript words cannot be used as names
A value to be assigned to the variable.
More Examples
Use let to assign 5 to x and 6 to y, and display x + y:
let y = 6;
document.getElementById("demo").innerHTML = x + y;
Declare many variables in one statement.
Start the statement with let and separate the variables by comma:
age = 30,
job = "carpenter";
Use let in a loop:
for (let i = 0; i < 5; i++) {
text += i + "<br>";
}
Browser Support
let
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |