🎓 Free Capstone Projects with Full Documentation, ER Diagrams & Source Code — Updated Weekly for 2026
👨‍💻 Free Source Code & Capstone Projects for Developers

What is Let in JavaScript

In the JavaScript Language, there are different keywords that developers utilize to declare and define variables.

One of the keyword is “let“. In this article, we will discuss the ins and outs of the “let” keyword in JavaScript and understand its significance in modern web development.

So, what is ‘let’ in JavaScript exactly? Let’s proceed in and find out.

What is the Meaning of Let in JavaScript?

In JavaScript, the “let” keyword is used to declare block-scoped variables.

It was popularized in ECMAScript 6 (ES6) to solve several issues related to variable scope and raise in JavaScript.

Before the arrival of “let“, developers depend on the “var” keyword to declare variables.

However, “var” have some characteristics that “let” effectively resolves.

The Difference between ‘let’ and ‘var’

1. Hoisting

One of the key differences between “let” and “var” lies in how they are hoisted.

When variables declared with “var” are hoisted, they are lead to the top of their scope, making them accessible throughout the entire scope.

Additionally, variables declared with “let” are hoisted as well but they persist inaccessible until they are declared in the code.

This enables for better control over variable declaration and prevents possible issues caused by hoisting.

2. Block Scoping

The variables declared with “var” have function-level scope while variables declared with “let” have block-level scope.

Block-level scope means that a variable declared with “let” is only available within the block of code it is defined in, such as inside a loop or conditional statement.

Once the block is exited, the variable is no longer available, increasing the code accuracy and reducing possible bugs.

3. Redeclaration

Variables declared with var can be redeclared within the same scope with no any warnings or errors.

This can lead to unexpected consequences and hard-to-debug issues.

On the other hand, variables declared with “let” cannot be redeclared within the same scope.

If you try to redeclare a variable with ‘let‘, a syntax error will be trigger, alerting you to the issue and promoting better coding practices.

How to Use ‘let’ in JavaScript?

Now that we have understand the basic of the ‘let‘ keyword, let’s proceed on how it is used in practice.

To declare a variable using ‘let‘, simply use the keyword followed by the variable name and an alternative assignment.

Here’s an example:

let samplecount = 0;

Here, we have declared a variable named ‘samplecount‘ and assigned it the initial value of 0.

The variable ‘samplecount‘ is block-scoped, which means it is only available within its consisting block.

You can also declare numerous variables using ‘let‘ in a single statement, separating them with commas.

For example:

let sampleFirstName = 'Rodrigo', sampleLastName = 'Flores', age = 41;

In this example, we declare three variables such as “sampleFirstName“, “lastName” and “age” are all using the ‘let‘ keyword.

Common Use Cases for ‘let’

Loop Variables

One of the common use cases for the ‘let‘ keyword is within loops, where variables usually need to have a specific value for each iteration.

Here is the following example:

for (let x = 0; x< 10; x++) {
  console.log(x);
}

In this ‘for loop’, the variable ‘x‘ is declared using ‘let‘ and has block scope limited to the loop’s body.

As a result, each iteration of the loop has its own ‘x‘ variable, persisting the expected action.

Block-Scoped Variables

As mentioned earlier, ‘let‘ enables for block-scoped variables. This feature is especially useful when dealing with conditional statements and nested blocks of code.

By using ‘let‘ to declare variables within specific blocks, you can ensure their appropriate scoping and prevent any unexpected side effects.

Here’s an example code:

function mySampleFunction() {
  if (true) {
    let i = 12;
    console.log(i);
  }
  // i is not accessible here
}

In this example, the variable ‘i‘ is declared using ‘let‘ within the ‘if‘ block.

As a result, ‘i‘ is only available within that block and cannot be accessed outside of it.

Reducing Global Namespace Pollution

Another advantage of using ‘let‘ is that it helps decrease global namespace pollution.

By declaring variables with ‘let‘ inside a block, you restrict their scope to that block, avoiding them from polluting the global scope.

This promotes cleaner code and minimizes the chances of variable name clashes or unexpected changes of global variables.

let vs const vs var — Quick Decision Guide

KeywordReassignable?Block Scoped?Use When
constNoYesDEFAULT — use this 90% of the time
letYesYesCounter or value that changes
varYesNo (function-scoped)Almost never — legacy code only

Modern best practice in 2026: use const by default. Switch to let only when you need to reassign. Never use var in new code — its function-scoping rules cause subtle bugs.

The Temporal Dead Zone (TDZ)

// var — hoisted with undefined
console.log(a);   // undefined
var a = 5;

// let — hoisted but in "temporal dead zone"
console.log(b);   // ❌ ReferenceError
let b = 5;

// const behaves like let
console.log(c);   // ❌ ReferenceError
const c = 5;

The TDZ is the period between when a let/const variable is declared (at the top of the block) and the actual let x = ... line. Accessing the variable during this window throws ReferenceError. This is intentional — it catches the kind of bugs that var hoisting silently allowed.

Block Scoping Saves You From Loop Bugs

// ❌ Classic var bug — every callback sees the SAME i
for (var i = 0; i < 3; i++) {
    setTimeout(() => console.log(i), 100);
}
// Output: 3, 3, 3

// ✓ With let — each iteration has its own i
for (let i = 0; i < 3; i++) {
    setTimeout(() => console.log(i), 100);
}
// Output: 0, 1, 2

This bug used to require workarounds like IIFE wrappers in the var days. With let, block scoping just works. Each loop iteration creates a fresh binding of i, so callbacks see the value at their creation time.

Common let Mistakes

  • Using let when const would do. If the variable is never reassigned, declare it as const. This makes intent clear and catches accidental reassignment.
  • Declaring at the top “just in case”. With let, declare close to first use. Hoisting all declarations to the top is a var-era habit that hurts readability.
  • Redeclaring let in the same scope. let x = 1; let x = 2; throws SyntaxError. Unlike var, let cannot be redeclared in the same block.
  • Forgetting let exists in for headers. The variable declared with let in a for loop only exists inside the loop. Trying to access it after throws ReferenceError.
  • Using let for module-level constants. Anything declared once and never changed at module scope should be const. let API_URL = "..." tells future readers the URL might change — it should not.

Additional Q&A (Add to Existing Schema, Do Not Duplicate)

Why use let instead of var in 2026?

Block scoping. The let keyword respects {} blocks, while var only respects function boundaries. This eliminates the classic “setTimeout in a loop sees the wrong value” bug and prevents accidental leakage between blocks. Modern style guides (Airbnb, Google) recommend never using var in new code.

Can I redeclare a let variable?

Not in the same scope. let x = 1; let x = 2; in the same block throws SyntaxError. You CAN have a let x in an inner block that shadows an outer let x — that is shadowing, not redeclaration. var allowed redeclaration; let intentionally forbids it.

What is the temporal dead zone (TDZ) in JavaScript?

The temporal dead zone is the period between when a let or const variable is hoisted to the top of its block and when its declaration line actually executes. During this period, accessing the variable throws ReferenceError. The TDZ exists to catch bugs that var hoisting silently allowed.

Related JavaScript Tutorials

Conclusion

In conclusion, the ‘let’ keyword in JavaScript offers developers with an enhanced method for variable declaration and scoping.

By using the block-level scoping, ‘let’ enables better control over variables, reduces scope-related issues, and promotes cleaner code.

FAQs

Can I reassign a variable declared with ‘let’?

Yes, variables declared with ‘let’ can be reassigned within the same scope.

Can I declare a variable with the same name as an existing variable using ‘let’?

No, you cannot declare a variable with the same name in the same scope using ‘let.’ This would result in a syntax error.

Can I use ‘let’ and ‘var’ together in the same code?

Yes, you can use ‘let’ and ‘var’ together in the same code. However, it is regularly recommended to use ‘let’ frequently to avoid possible scope-related issues and promote code clarity.

Is ‘let’ keyword case-sensitive in JavaScript?

No, the ‘let’ keyword is not case-sensitive in JavaScript. Both ‘let’ and ‘LET’ can be used interchangeably.

Additional Resources

Leave a Comment