Prev Page Next Page
Top

JavaScript Let

➢The let keyword was introduced in ES6 (2015).
➢Variables defined with let cannot be Redeclared.
➢Variables defined with let must be Declared before use.
➢Variables defined with let have Block Scope.

Cannot be Redeclared

➢Variables defined with let cannot be redeclared.
➢You cannot accidentally redeclare a variable.

EXAMPLE:

let x = "BMR";
let x = 0;

// SyntaxError: 'x' has already been declared

Block Scope

➢Before ES6 (2015), JavaScript had only Global Scope and Function Scope.
➢ES6 introduced two important new JavaScript keywords: let and const.
➢These two keywords provide Block Scope in JavaScript.
➢Variables declared inside a { } block cannot be accessed from outside the block:

EXAMPLE:

{
let x = 0;
}
// x can NOT be used here

Redeclaring Variables

➢Redeclaring a variable using the let keyword can solve this problem.
➢Redeclaring a variable inside a block will not redeclare the variable outside the block:

EXAMPLE:

let x = 10;
// Here x is 10
{
let x = 5;
// Here x is 5
}
// Here x is 10

Prev Page Next Page





FEEDBACK

Rating


Message