JavaScript Let Variable Declaration

Declare Variables Using Let In JavaScript

The let keyword has been to declare variables since ECMAScript 2015 (ES6).

JavaScript variables can be declared in 4 ways. Automatically, using var, let or const.

Let is used to declare variables in a function or global scope with a Temporal Dead Zone (TDZ).

Variable Declaration

Glossary:

Scope

Refers to region of the code where variable is accessible.

Global Scope

Variables declared outside function or block are accessible anywhere.

Block Scope

Variables declared within block such as if or for statement are only accessible in that local scope.

Function Scope

Variables declared within function are only accessible in that local scope.

Initialization

Variables are initialized with a default value.

Reassignable

Variables are reassigned in the same scope.

Temporal Dead Zone

Variables are not accessible until execution reaches declaration point.

Declaring Variables

Let Variable Declaration
Name Description Example
let Declares function scope or global scope with optional initialization let num = 1;
Name Description Example

Let Variable Declaration Snippet

// OjamboShop.com Learning JavaScript Let Variable Declaration Tutorial
// Global Scope
let num1;
num1 = 1;
console.log(num1);
// Inaccessible Outside Block
if (true) {
	console.log(num1);
	let num2 = 2;
	console.log(num2);
}
// Reassign Variable
num1 = 11;
let num2 = num1;
num2 = 22;
console.log(num1);
console.log(num2);

JavaScript Let Variable Declaration Code
OjamboShop.com Web IDE JavaScript Let Variable Declaration Code

JavaScript Let Variable Declaration Result
OjamboShop.com Web IDE JavaScript Let Variable Declaration Result


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE was used to input and compile JavaScript code for the let variable declaration.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Learn Programming Courses:

Courses are optimized for your web browser on any device.

OjamboShop.com Learning JavaScript Course
OjamboShop.com Learning JavaScript Interactive Online Course

Limited Time Offer:

OjamboShop.com is offering 20% off coupon code SCHOOL for Learning JavaScript Course until End Day 2024.

Learn Programming Ebooks:

Ebooks can be downloaded to your reader of choice.

OjamboShop.com Learning JavaScript Ebook
OjamboShop.com Learning JavaScript Ebook Cover Page

Conclusion:

JavaScript makes it easy to declare variables. Use let to declare function or global scoped variables with the optional initialization.

Take this opportunity to learn the JavaScript programming language by making a one-time purchase at Learning JavaScript Course. A web browser is the only thing needed to learn JavaScript in 2024 at your leisure. All the developer tools are provided right in your web browser.

If you prefer to download ebook versions for your reader then you may purchase at Learning JavaScript Ebook

References: