JavaScript let

Example

Create a variable called carName and assign the value "Volvo" to it:

let carName = "Volvo";
Try it Yourself »

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:

let carName;

After the declaration, the variable is empty (it has no value).

To assign a value to the variable, use the equal sign:

carName = "Volvo";

You can also assign a value to the variable when you declare it:

let carName = "Volvo";

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

let name = value;

Parameters

Parameter Description
name Required.
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
value Optional.
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 x = 5;
let y = 6;
document.getElementById("demo").innerHTML = x + y;
Try it Yourself »

Declare many variables in one statement.

Start the statement with let and separate the variables by comma:

let lastName = "Doe",
age = 30,
job = "carpenter";
Try it Yourself »

Use let in a loop:

let text = "";
for (let i = 0; i < 5; i++) {
  text += i + "<br>";
}
Try it Yourself »

Browser Support

let is an ECMAScript6 (ES6) feature.

ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:

Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 Apr 2017 Jun 2017 Sep 2016 Jun 2016

let is not supported in Internet Explorer.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.