The keyword 'let' was added in ECMAScript 2015 and can be used as an alternative to 'var'.
A variable declared using "let" has block scope.
A variable declared using "let" is hoisted to the top of its scope, however it is not initialised until declaration.
A variable declared using "let" cannot be accessed before it has been declared because the hoisting does not initialize it with a value.
let variables are not initialised (and are only initialised when they are assigned a value).
let variables cannot be used before they have been declared.
If you try and access it before it has been declared you will get a ReferenceError.
These variable are said to be in "temporal dead zone" from the start of the block until the initialization is processed.
This variable can be reassigned.

function myFunction() { 
   var x = 100;
   if (true) {
      let x = 200;
   console.log(x); // x is 100

The let statement creates a new block scoped variable.


It is not possible to redeclare an existing variable as a let in the same scope.
(regardless of whether it was declared with a var or a let)
Cannot be redeclared within its scope

Inside a For Loop

Declaring a variable with 'let' locks the value during each iteration of the loop.


Allows you to declare and assign on the same line
(this is not possible in JavaScript ?)

© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext