JavaScript Code

JavaScript let


let keyword

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
}