let keyword
In JavaScript, let
keyword is used to declare variables.
let x = "apple";
Variables declared using let
keyword cannot be redeclared in the block-scope.
{
let x = "apple";
//x can be used in this block
}
//x cannot be used here
Variables defined using let
keyword must be declared before using them in program.
{
x = 5;
let x = "apple"; //Uncaught ReferenceError: Cannot access 'x' before initialization
}
Variables declared using let
keyword have block scope.
{
let x = "apple";
//x is apple
{
let x = "banana";
//x is banana
}
//x is apple
}