JavaScript let is a keyword used to declare variables that are block-scoped. Variables defined with the let keyword cannot be redeclared and must be declared before use.
let keyword was added in the ES6 or ES2015 version of JavaScript. Generally, it is suggested that we use the let keyword while working with JavaScript.
Syntax:
let variable_name = value;
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;
console.log(num)
}
console.log(num)
|
Output:
10
Uncaught ReferenceError: num is not defined
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();
|
Output:
10
10
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();
console.log(num);
|
Output:
10
"ReferenceError: num is not defined
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
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;
console.log(x);
}
let x = 67;
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?
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, 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!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
13 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...