← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 8 of 12

JS Let

Imagine a meeting room that's only booked and available during a specific meeting, once that meeting ends, the room and anything left inside it are gone, unavailable to any other meeting outside that time slot. The let keyword creates variables that work the same way, they exist only within the block of code, the { } braces, where they were declared, whether that's an if statement, a loop, or a function. This block scoping makes let variables far more predictable than the older var, which is why it has become the standard choice for values that change, across projects big and small, including cookiescursor.com.
Syntax
javascript
let variableName = value;
variableName = newValue;

Block Scope

let variable केवल उसी सबसे नज़दीकी घुँघराले कोष्ठकों { } के भीतर मौजूद रहता है जिनमें वह है, चाहे वह if statement हो, loop हो या कोई अन्य code block, और उस block के बाहर पूरी तरह पहुँच से बाहर रहता है।

Note: Block scoping का मतलब है कि आप i या item जैसे सरल नामों को अलग-अलग blocks में एक-दूसरे में दख़ल दिए बिना सुरक्षित रूप से दोबारा इस्तेमाल कर सकते हैं।
Warning: A let variable declared inside an if block cannot be used after that block ends, even later in the same function.

उदाहरण: Block Scope

javascript
if (true) {
  let message = "Inside the block";
  console.log(message);
}
// console.log(message); // ReferenceError: message is not defined

दोबारा Declaration नहीं

JavaScript एक ही scope में एक ही let variable नाम को दो बार घोषित करने की अनुमति नहीं देती, यह प्रतिबंध अनजाने में होने वाले नामों के टकराव पकड़ने में मदद करता है जो वरना उलझन भरे bugs पैदा कर सकते थे।

Note: अगर let के साथ आपको redeclaration error मिले, तो जाँचिए कि क्या आपका असली मतलब मौजूदा variable को सिर्फ़ दोबारा assign करना था।
Warning: Attempting to redeclare a let variable in the same scope throws a SyntaxError immediately, before the script even runs.

उदाहरण: No Redeclaration

javascript
let name = "Sam";
// let name = "Alex"; // SyntaxError: already declared
name = "Alex"; // reassignment is fine
console.log(name);

Temporal Dead Zone

Temporal dead zone किसी block की शुरुआत और उस असली line के बीच का समय है जहाँ let variable घोषित होता है, जिस दौरान variable तकनीकी रूप से मौजूद होता है पर उसे एक्सेस नहीं किया जा सकता, और ऐसा करने पर error आती है।

Note: temporal dead zone errors से बचने का सबसे सुरक्षित तरीका बस हमेशा variables को उस block के ऊपर के पास घोषित करना है जहाँ वे इस्तेमाल होते हैं।
Warning: Unlike var, which quietly returns undefined if accessed early, let throws a clear error, which is actually a helpful safety net.

उदाहरण: The Temporal Dead Zone

javascript
// console.log(value); // ReferenceError: Cannot access before initialization
let value = 10;
console.log(value);

Loops के अंदर let

क्योंकि let block-scoped है, loop की हर iteration को let variable की अपनी नई प्रति मिलती है, जो loop के अंदर बनाए गए functions, जैसे event handlers या timers, के साथ काम करते समय बहुत मायने रखता है।

Note: जब आपको setTimeout जैसे callbacks के अंदर loop counter से पूर्वानुमेय व्यवहार चाहिए, तो let ज़रूरी है, var इसके बजाय चौंकाने वाले साझा values दे सकता है।
Warning: Using var instead of let for a loop counter used inside a delayed function often causes every callback to see the same final value.

उदाहरण: let Inside Loops

javascript
for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
// Logs 0, 1, 2 - each iteration gets its own i

let बनाम var कब इस्तेमाल करें

आधुनिक JavaScript में, जिस भी value को बदलना पड़ता हो उसके लिए let आपकी डिफ़ॉल्ट पसंद होनी चाहिए, इसके block scoping और सख़्त नियम bugs की पूरी श्रेणियों को रोकते हैं जिन्हें var अनुमति देता है, जिससे पूरी टीम में code ज़्यादा पूर्वानुमेय बनता है।

Note: अगर आप पुराना code संभाल रहे हैं जो var इस्तेमाल करता है, तो जिन हिस्सों को आप छूते हैं उनमें धीरे-धीरे उसे let या const से बदलने में आमतौर पर कोई हर्ज नहीं।
Warning: Mixing var and let for similar purposes in the same file can create inconsistent scoping behavior that's confusing to reason about.

उदाहरण: When to Use let vs var

javascript
// Declare the variable `total`, set to `0`
// Declare the variable `total`, set to `0`
let total = 0;
// Classic for-loop: `let i = 1; i <= 3; i++`
// Classic for-loop: `let i = 1; i <= 3; i++`
for (let i = 1; i <= 3; i++) {
  total += i;
}
// Print `total` to the console
// Print `total` to the console
console.log(total);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.