पाठ 23 / 40

स्कोप और Hoisting

Global, function और block scope, hoisting तथा let व const के temporal dead zone को समझें।

फ़ंक्शन बनाम ब्लॉक स्कोप

वेरिएबल का scope यह तय करता है कि वह कहाँ दिखता है: var घेरने वाले फ़ंक्शन तक सीमित है, जबकि let/const घेरने वाले { } ब्लॉक तक।

Hoisting

Compilation के दौरान घोषणाएँ अपने scope के ऊपर ले जाई जाती हैं — पर सिर्फ़ घोषणा, असाइनमेंट नहीं।

console.log(x);   // undefined, not an error
var x = 5;

console.log(y);   // ReferenceError
let y = 5;

टेम्पोरल डेड ज़ोन

let/const भी hoisted होते हैं, पर अपनी लाइन चलने तक "temporal dead zone" में रहते हैं — पहले एक्सेस करने पर त्रुटि आती है।