JavaScript let

JavaScript let is a keyword used to declare variables in block scoped JavaScript. Two new keywords have been added in JavaScript version ES6 or ES2015. In general, we recommend using the let keyword when working with JavaScript.

Syntax:

let variable_name = value;

Block Scope: Variables declared inside block { } are called block scope variables whereas variables declared with the var keyword cannot be block scoped.

Example: In this example, the variable num is block scoped and it is not accessible outside the block. If we try to access the variable outside the block, it will throw a reference error.

JavaScript

{
    let num=10;
    // calling the function inside block
    console.log(num)
}
// calling the function outside block throws a Error
console.log(num)

Output:

10
Uncaught Reference Error: num is not defined.

Global Scope: A global scope variable is a variable declared in the main body of the source code, outside all the functions.

Example: In this example, the marks variable is a globally scoped variable and it can be accessed from anywhere in the program.

JavaScript

let marks=50;
console.log(marks);
function fun(){
    console.log(marks);
}
fun(); // calling the function

Output:

50 
50

Function Scope: A function scope variable is a variable declared inside a function and cannot be accessed outside the function.

Example: In this example, the num variable is declared inside the function and cannot be accessed outside the function.

JavaScript

function fun(){
    let num=10;
    console.log(num);
}
fun(); //  calling the function
console.log(num);

Output:

10
"Reference Error: num is not defined.

Redeclaration of Variables in different blocks: The variables declared using let keyword can be redeclared inside other blocks.

Example: In below example, variable x is redeclared inside other blocks.

JavaScript

let x=77;
{
    let x=23;
    console.log(x);
}
console.log(x);

Output:

23
77

Redeclaration of Variables in the same blocks: We cannot redeclare variables using the let keyword inside the same blocks. It will throw an error.

Example: In the example below, variable x is redeclared inside same blocks.

JavaScript

let x=77;
    {
        let x=23; // legal
        console.log(x);
    }
    let x=67;// illegal
    console.log(x);

Output:

Uncaught Syntax Error: Identifier 'x' has already been declared.

Does not support Hoisting: The behavior of moving the declarations on top of the script is known as hoisting.

Example: Let does not support hoisting.

JavaScript

x=12;
    console.log(x);
    let x;   

Output:

Uncaught Reference Error: Cannot access 'x' before initialization. 

Supported Browser:

  • Chrome 49 and above
  • Edge 14 and above
  • Firefox 44 and above
  • Opera 17 and above
  • Internet Explorer 11 and above
  • Safari 10 and above

Note:  To clear your concept of var, and const, and let please go through How to declare variables in different ways in JavaScript?

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, CodeConfig.in Courses are your key to success. We provide top-quality content at no cost, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!