ScanSkill

let

The let statement is used to declare a variable of block scope.

Syntax

let variableName;

The let keyword is associated with a name. The name is attached to the right side of the let keyword, the name becomes a variable name.

Example

let hello = "Ola"
if(true) {
  let hello = "Hello"; // a different variable
  console.log(hello); // prints: Hello
}

console.log(hello); // prints: Ola

hello = "Hi";
console.log(hello); // prints: Hi

The let declarations can be declared once on a block. The duplicate declarations will trigger an error. Variable declared from let can be assigned to different values multiple times. The let statement is used to declare a variable such that its value of it can change throughout the block.