In this chapter, we will learn about the let
keyword in JavaScript. We will cover:
- What is
let
? - Syntax
- Declaring Variables with
let
- Block Scope
- Temporal Dead Zone
- Re-declaration Restrictions
let
vsvar
What is let?
let
is a keyword used to declare variables in JavaScript. Introduced in ES6 (ECMAScript 2015), let
provides block scope and helps prevent some common pitfalls associated with var
.
Syntax
let variableName = value;
let
is the keyword.variableName
is the name of the variable.value
is the initial value assigned to the variable (optional).
Declaring Variables with let
You can declare a variable using let
followed by the variable name and an optional initial value.
Example
let name = "Ramesh"; console.log(name); // Output: Ramesh let age; age = 25; console.log(age); // Output: 25
In the example above, the variable name
is declared and initialized with the value "Ramesh"
. The variable age
is declared without an initial value and then assigned the value 25
.
Block Scope
Variables declared with let
are block-scoped, meaning they are only accessible within the block they are declared in (e.g., inside {}
). This is different from var
, which is function-scoped.
Example
if (true) { let blockScoped = "This is block scoped"; console.log(blockScoped); // Output: This is block scoped } // console.log(blockScoped); // This will cause an error because blockScoped is not accessible outside the block
In the example above, the variable blockScoped
is only accessible within the if
block.
Temporal Dead Zone
The temporal dead zone (TDZ) is a behavior where variables declared with let
cannot be accessed before they are declared in the code.
Example
// console.log(name); // This will cause an error because name is in the TDZ let name = "Ramesh"; console.log(name); // Output: Ramesh
In the example above, accessing name
before its declaration will cause an error.
Re-declaration Restrictions
Unlike var
, variables declared with let
cannot be re-declared within the same scope.
Example
let name = "Ramesh"; // let name = "Raj"; // This will cause an error because name has already been declared name = "Raj"; // This is allowed because we are updating the value, not re-declaring the variable console.log(name); // Output: Raj
In the example above, re-declaring the variable name
with let
will cause an error, but updating its value is allowed.
let vs var
Key Differences
-
Scope:
let
is block-scoped.var
is function-scoped.
-
Hoisting:
- Variables declared with
let
are hoisted but not initialized, leading to the temporal dead zone. - Variables declared with
var
are hoisted and initialized withundefined
.
- Variables declared with
-
Re-declaration:
let
does not allow re-declaration within the same scope.var
allows re-declaration within the same scope.
Example
function example() { if (true) { let letVariable = "let variable"; var varVariable = "var variable"; } // console.log(letVariable); // This will cause an error because letVariable is block-scoped console.log(varVariable); // Output: var variable (varVariable is function-scoped) } example();
In the example above, letVariable
is not accessible outside the if
block, while varVariable
is accessible because it is function-scoped.
Conclusion
In this chapter, you learned about the let
keyword in JavaScript. We discussed how to declare variables with let
, its block scope, the temporal dead zone, re-declaration restrictions, and the differences between let
and var
. Understanding let
is crucial for writing modern and error-free JavaScript code. In the next chapter, we will explore the const
keyword and its uses in JavaScript.