What is javascript hoisting in hindi

javascript hoisting in hindi

Introduction

हेलो दोस्तों आज हम इस पोस्ट में javascript के Hoisting concepts example के साथ detail से समजेंगे!

 

What is javascript hoisting in hindi

 

Hoisting एक ऐसी विशेषता है जिसके कारण Javascript में लिखे गए variable या function को उनके scope के ऊपर ले जाया जाता है। इसका मतलब होता है कि जब भी आप एक variable या function को declared करते हो, तो उसे अपने scope के शुरुआत में ले जाया जाता है, जिससे variable या function को उस scope में available होने से पहले भी उस scope के अंदर के code में इस्तेमाल किया जा सकता है।

यह कार्य Javascript के Interpreted द्वारा किया जाता है जो एक नये scope में जाने से पहले सभी variable और function को उस scope के शुरुआत में ले जाता है।

इसका ध्यान रखें कि Hoisting केवल variable declarations या function declarations को ही ले जाता है और न कि उनकी value को। इसलिए आप जब भी variable या function का इस्तेमाल करते हो तो उनकी value उस scope में available होनी चाहिए।

Example:

<script>
 console.log(myVariable); // undefined
 var myVariable = 10;
</script>

इस code में, हम myVariable के value को console में log करने की कोशिश कर रहे हैं जो कि इससे पहले declared नहीं हुआ है। normally यह एक ReferenceError generate करता है,

लेकिन Hoisting के कारण, code को इस प्रकार declared किया जाता है:

<script>
  var myVariable;
  console.log(myVariable); // undefined
  myVariable = 10;
</script>

var myVariable declared अपने scope के top पर host की जाती है, जो इस मामले में global scope है (क्योंकि यह किसी function में declared नहीं हुआ है)। इसलिए, हम console में myVariable को log करने की कोशिश करते हैं,
तो इसे पहले से ही declared कर दिया गया होता है और undefined का एक value दिया जाता है।

Function declaration के साथ भी hoisting काम करती है,

For  Example:

<script>
 myFunction(); // "Hello, world!"
 function myFunction() {
  console.log("Hello, world!");
 }
</script>

यहां, हम myFunction() को declared होने से पहले call करने की कोशिश कर रहे हैं। Normally यह एक ReferenceError generate करता है, लेकिन Hoisting के कारण, ReferenceError generate नहीं करता है|

Hoisting काम करता है, लेकिन यह scope के प्रकार के आधार पर बदल जाता है। आप जानते होंगे कि JavaScript में दो प्रकार के scope होते हैं: Global scope और local scope.
जब आप एक function के भीतर एक variable या function का नाम declared करते हैं, तो वह सिर्फ उस function के local scope में host होता है, जबकि global scope में नहीं।

Conclusion

 

उम्मीद है कि आपको Hoisting के बारे में अब समझ में आ गया होगा। Hoisting के अलावा, जब आप एक variable को declared करते हो और उसे कोई value नहीं देते हो तो उस variable की default value undefined होती है। यह एक और javascript की विशेषता है जिसे default वैल्यू नाम दिया जाता है।

जब आप एक function को declared करते हो, तो उस function के नाम को उसके scope में host कर दिया जाता है, लेकिन function को उस scope में available नहीं कराया जाता है। इसलिए, जब आप function को call करते हो तो, उसे उस scope में available होना चाहिए।

उम्मीद है कि आपको यह समझ में आ गया होगा कि hoisting क्या है और यह कैसे काम करता है। Hoisting के बारे में जानना बहुत important है, क्योंकि इससे आपको यह बताने में मदद मिलती है कि आपकी Javascript code का कैसे interpriter उसे पढ़ता है और कैसे वह code load होता है।

 

Leave a Comment

Your email address will not be published. Required fields are marked *