DEV Community

Sirajul Islam
Sirajul Islam

Posted on

1

JavaScript Hoisting

Hoisting কি?
JavaScript-এ hoisting হলো একটি মেকানিজম যেখানে variable এবং function declarations গুলো automatically স্কোপের শীর্ষে (top of scope) উঠে যায় execution করার আগে। অর্থাৎ, আপনি যদি কোনো ভেরিয়েবল বা ফাংশন ডিক্লেয়ারেশন কোডের পরে ব্যবহার করেন, তবুও JavaScript এটি কাজ করবে।

Hoisting কিভাবে কাজ করে?
Variable hoisting: শুধুমাত্র declaration উপরে উঠে যায়, value (initialization) উপরে ওঠে না।
Function hoisting: Function declarations সম্পূর্ণরূপে উপরে ওঠে, যার ফলে আপনি ডিক্লেয়ারের আগেও ফাংশন কল করতে পারেন।

Variable Hoisting:
variable hoisting
কেন undefined আসলো?
👉 কারণ JavaScript কোড চালানোর আগে নিচের মতো করে পরিবর্তন করে নেয়:
variable hoisting 2
👉 let এবং const hoisting করে, কিন্তু তারা টেম্পোরাল ডেড জোন (TDZ) তৈরি করে, যার ফলে ব্যবহার করার আগে ReferenceError দেখায়।

Function Hoisting:
function hoisting
👉 এখানে পুরো function declaration উপরে ওঠে, তাই এটি ডিক্লেয়ারের আগেও কল করা যায়।

Function Expression এ Hoisting হয় না:
function hoisting
👉 এখানে শুধুমাত্র var greet; উপরে উঠবে, কিন্তু function definition না, তাই এটি TypeError দেখাবে।

সংক্ষেপে

  1. var hoisting হয়, কিন্তু undefined হয়।
  2. let & const hoisting হয়, কিন্তু TDZ এর কারণে ReferenceError দেয়।
  3. Function declarations পুরোপুরি hoisting হয়।
  4. Function expressions hoisting হয় না।

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay