JavaScript Hoisting In Hindi | Hoisting In JavScript

Image could not load

JS hoisting

What is hoisting

जब भी JavaScript code run होता है तो एक execution context create हो जाता है जो define किये गए Function और Variables के लिए memory allocate करने में help करता है और जिससे पता चलता है कि currently कौन सा code run हो रहा है।

एक Execution Context के अंदर functions और variable declarations के लिए memory allocate करने की mechanism को ही Hoisting कहा जाता है।

JS Variable Hoisting

javascript में variable hoisting को सिर्फ var keyword के through define किये गए variables ही करते हैं।

For Example

console.log(x); var x = 65; console.log(x);
undefined
65

So , example के according normally hoisting का मतलब है define करने से पहले variables या functions को use करना।

अब obviously variable को define करने से पहले use करोगे तो actual value की जगह आपको undefined मिलेगा , जैसा कि example का output भी है।

JS execution context

आगे बढ़ने से पहले हम ये समझ लेते हैं , कि JavaScript hoisting manage करने के लिए execution context को कैसे create करता है।

basically , Execution context को दो phases में divide किया गया है -

  1. Memory Allocation : इस phase में variables और functions को memory allocate होती है।

  2. execution phase : जबकि execution phase में normally hoisted variables को एक initial value undefined assign की जाती है , जब तक वो variable को actual value assign नहीं होती है।

JS Function Hoisting

JavaScript में आप Functions declarations भी hoisted होता है , मतलब declaration से पहले function को call कर सकते हैं।

For Example

sayHi(); function sayHi(){ console.log("Hi !"); }
Hi !

यहां function , memory allocation phase store हो रहा है , जिसकी वजह से हम उस function को declaration से पहले ही call कर सकते हैं।

ध्यान रहे function hoisting , normal functions पर ही work करती है , Variable Function या Arrow Function पर hoisting apply नहीं होती है।

Example

// Arrow Function. sayHi(); var sayHi = () => console.log("Hi !");
Uncaught TypeError: sayHi is not a function

// Anonymous/Variable Function. sayHi(); var sayHi = function(){ console.log("Hi !"); }
Uncaught TypeError: sayHi is not a function

JS Temporal Dead Zone

temporal dead zone (TDZ) एक block of area होता है , जिसमे कोई भी variable तब तक accessible नहीं होता जब तक computer उस variable को completely initialize नहीं करता।

इसीलिए JavaScript में let और const keyword की help से define किये गए सभी variables को declare किये बिना use नहीं किया जा सकता है , या आप कह सकते हैं ये hoisting को support नहीं करते।

For Example

console.log(x); // Start of Temporal dead zone. let x = 10; // end of temporal dead zone
Uncaught ReferenceError: can't access lexical declaration 'x' before initialization
Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook