ЁЯУЪ

Introduction to JavaScript Hosting

May 23, 2025

Introduction to Hoisting in JavaScript

Key Points

  • There is a variable x with a value of 7.
  • There is a function getName that calls console.log(x).
  • Expected output: 7.

Hoisting in JavaScript

  • In JavaScript, you can access variables and functions even before they are declared.
  • Example: Calling getName() prints 7.
  • This is a specific behavior called hoisting.

Error Handling

  • If you try to access x without setting a value, you'll get an "undefined" error.
  • In JavaScript, accessing a variable before declaring it returns "undefined".

Characteristics of Function Declarations

  • getName is a function, but if converted to an arrow function, it will behave like a variable.
  • According to hoisting rules, it will not be treated as a function.

Memory Allocation

  • When JavaScript starts running the code, memory is allocated for functions and variables.
  • In memory, "undefined" is a special placeholder for variables.

Call Stack and Execution Context

  • When a function is called, a new execution context is created.
  • It is added to the call stack and removed when the function finishes.

Conclusion

  • Hoisting is an important aspect of JavaScript, determining when and how we can access variables and functions.
  • Keep this in mind next time you discuss hoisting.

Next Classes

  • Detailed discussion on functions in JavaScript will be conducted.
  • DonтАЩt forget to watch previous videos to learn more about this topic.