The Wayback Machine - https://web.archive.org/web/20240713063300/https://www.geeksforgeeks.org/javascript-let/
Open In App

JavaScript Let

Last Updated : 05 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The let keyword in JavaScript is used to make variables that are scoped to the block they’re declared in. Once you’ve used let to define a variable, you cannot declare it again within the same block. It’s important to declare let variables before using them.

The let keyword was introduced in the ES6 or ES2015 version of JavaScript. It’s usually recommended to use let when you’re working with JavaScript.

Syntax:

let variable_name = value;

1. Block Scope

The variables which are declared inside the { } block are known as block-scoped variables. variables declared by the var keyword cannot be block-scoped.

Example: In this example, the num variable is block-scoped and it cannot be accessed outside the block. If we try to access the variable outside the block it throws a reference error.

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

// Calling a function outside
// block throws an Error
console.log(num)

Output:

10
Uncaught ReferenceError: num is not defined

2. Global Scope

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

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

javascript
let num = 10;
console.log(num);
function fun() {
    console.log(num);
}

fun(); // Calling the function

Output:

10 
10

3. 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
"ReferenceError: num is not defined

1. Redeclaring Variables in different blocks

The variables declared using let can be redeclared inside other blocks.

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

javascript
let x = 77;

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

console.log(x);

Output:

23
77

2. Redeclaring Variables in the same blocks

We cannot redeclare variables using the let keyword inside the same blocks. It will throw an error.

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

javascript
let x = 77;

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

let x = 67; // illegal

console.log(x);

Output:

Uncaught SyntaxError: 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 do not support hoisting.

javascript
x = 12;

console.log(x);

let x;

Output:

Uncaught ReferenceError: Cannot access 'x' before initialization 

Supported Browser:

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

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



Previous Article
Next Article

Similar Reads

What is the use of let & const in JavaScript ?
In this article, we are going to discuss the usage of let and const in JavaScript with the help of certain theoretical explanations along with some coding example as well. let: let keyword is used to declare variables in JavaScript that are actually to made as block-scoped i.e. it allows us to declare a variable within a block or within an expressi
5 min read
Difference between var and let in JavaScript
In the early days of JavaScript, there was only one way of declaring variables and that was using the var keyword. A variable declared with var is defined throughout the program. One of the issues with using the var keyword was redeclaring a variable inside a block will also redeclare the variable outside the block.  With the introduction of ES6 in
3 min read
Difference between var, let and const keywords in JavaScript
The keywords var, let, and const in JavaScript define the variable scope and behavior. The var keyword has function scope and is hoisted. The let and const keywords have block scope, with const requiring an initial value and preventing reassignment. JavaScript var keywordThe var is the oldest keyword to declare a variable in JavaScript. It has the
5 min read
What is the meaning of let-* in Angular 2 Templates ?
The let keyword in Angular declares a template input variable that is referenced within the template. In Angular, the micro syntax is used to configure a directive in a compact and friendly string. It is a capability given in angular to embed the variables in the template directly by translating that string into attributes on the <ng-template
3 min read
Ember.js Ember.Templates.helpers let() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
2 min read
JavaScript Course Understanding Code Structure in JavaScript
Inserting JavaScript into a webpage is much like inserting any other HTML content. The tags used to add JavaScript in HTML are <script> and </script>. The code surrounded by the <script> and </script> tags is called a script blog. The 'type' attribute was the most important attribute of <script> tag. However, it is no
4 min read
Introduction to JavaScript Course - Learn how to build a task tracker using JavaScript
This is an introductory course about JavaScript that will help you learn about the basics of javascript, to begin with, the dynamic part of web development. You will learn the basics like understanding code structures, loops, objects, etc. What this course is about? In this course we will teach you about the basics of the scripting language i.e) Ja
4 min read
JavaScript Course Loops in JavaScript
Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. For example, suppose we want to print “Hello World” 10 times. Example: In this example we will print the same things, again and again, to understand the work of Loops. <script> cons
4 min read
JavaScript Course Logical Operators in JavaScript
logical operator is mostly used to make decisions based on conditions specified for the statements. It can also be used to manipulate a boolean or set termination conditions for loops. There are three types of logical operators in Javascript: !(NOT): Converts operator to boolean and returns flipped value &&:(AND) Evaluates operands and retu
3 min read
JavaScript Course What is JavaScript ?
JavaScript is a very famous programming language that was originally started in the year of 1995 with the motive of making web pages alive. It is also an essential part of the web developer skillset. In simple terms, if you want to learn web development then learn HTML & CSS before starting JavaScript. HTML: HTML is used to create the structure of
3 min read
three90RightbarBannerImg