Hoisting in JavaScript πŸ”₯(variables & functions) | Namaste JavaScript Ep. 3

Hoisting in JavaScript πŸ”₯(variables & functions) | Namaste JavaScript Ep. 3

Understanding Variable Access in JavaScript

Introduction to Variables and Functions

  • The speaker introduces a variable x initialized to 7 and a function getName that logs the value of x.
  • The expectation is set for the output when invoking the function, which should print 7 on the console.

Exploring JavaScript Behavior

  • The speaker discusses accessing variables before they are initialized, highlighting that this typically results in an error in most programming languages.
  • In contrast, JavaScript allows access to variables before initialization, demonstrating this with the function call that successfully prints its name.

Undefined Variables

  • If the line defining x is removed, an error occurs indicating that x is not defined. This emphasizes how JavaScript handles undefined variables differently.
  • The concept of "hoisting" in JavaScript is introduced, where functions and variables can be accessed even if they haven't been explicitly initialized yet.

Memory Creation Phase

  • The speaker explains that during execution, memory allocation happens for all variables and functions before any code runs.
  • A deeper dive into how memory is allocated reveals that even uninitialized variables have reserved space in memory marked as undefined.

Function Invocation and Console Logging

  • When logging a function itself without invoking it, JavaScript prints the function definition rather than executing it.
  • The discussion includes how calling a function or accessing a variable behaves differently based on whether they have been initialized or not.

Conclusion: Key Takeaways from JavaScript's Unique Handling of Variables

  • Understanding these concepts helps clarify why certain behaviors occur in JavaScript compared to other programming languages.

Understanding Undefined and Uninitialized Variables in JavaScript

The Concept of Undefined

  • The term "undefined" is introduced, explaining that it occurs when a variable has been declared but not assigned a value.
  • A distinction is made between "not defined" and "undefined," emphasizing that the former indicates a variable hasn't been declared at all.
  • When trying to access an undefined variable, JavaScript throws an error indicating that the reference is not present in memory.

Arrow Functions vs Regular Functions

  • The discussion shifts to arrow functions, specifically how they differ from regular function declarations. An example with getName as an arrow function illustrates this point.
  • It’s noted that getName behaves like a variable rather than a function, which affects how memory allocation occurs during execution.

Memory Allocation and Execution Context

  • During the memory allocation phase, both undefined variables and arrow functions are allocated space in memory without initial values.
  • The global execution context is created when running code, which reserves space for variables like getName.

Function Invocation and Call Stack

  • When invoking the getName function, a new execution context is created and pushed onto the call stack.
  • Only proper function declarations reserve memory for their entire code block; otherwise, they behave like uninitialized variables.

Hosting Mechanism in JavaScript

  • The concept of hoisting is explained: JavaScript moves declarations to the top of their scope during compilation but does not initialize them until runtime.
  • It's emphasized that understanding hoisting is crucial for interviews or discussions about JavaScript behavior.

Demonstration of Execution Context

  • A demo will illustrate how execution contexts work within JavaScript code by showing real-time examples.
  • The global execution context's structure will be examined further to understand its role in managing control flow during program execution.

Conclusion on Execution Flow

  • As functions are executed, new contexts are created while maintaining references to previous ones on the call stack.

Understanding JavaScript Execution Context

Overview of Call Stack and Execution Context

  • The discussion begins with the deletion of contacts and execution context, emphasizing that it will be removed from the call stack. This sets the stage for understanding how JavaScript manages function calls.
  • The speaker highlights the importance of a previous video that covered theory related to call stacks, suggesting viewers watch it for foundational knowledge. The current demo aims to illustrate these concepts in practice.
  • An explanation is provided about the science behind JavaScript's engine and its management of the call stack during execution. This involves understanding how various processes occur within the browser environment.
  • The focus shifts to functions as a critical component in JavaScript, noting their distinctiveness compared to other programming languages. Functions are presented as essential elements that will be explored further in upcoming discussions.

Engagement and Motivation

Video description

Hoisting in JavaScript is the most famous Interview Question. And it is also one of the most misunderstood concepts in JS. This video will cover in-depth knowledge and fundamentals behind variable and function hoisting. Hoisting as a core concept relies on the way how Execution Context is created. In the first phase i.e. the Memory Allocation Phase all the variables and functions are allocated memory, even before any code is executed. All the variables are assigned undefined at this point in time in the local memory. My tech gear I use every day - http://google.peek.link/2pba Topics Covered: 00:00 - Introduction to Hoisting in JavaScript (with examples) 00:54 - Variable and Function Hoisting in JS 03:02 - Definition of Hoisting 03:52 - Functions Hoisting Behind the Scenes 04:37 - Diving Deep under the hood of the JavaScript code 13:37 - Summary and Interview tip 14:12 - Demo of Call Stack in the Browser 18:23 - Teaser of the next video - How functions work in JavaScript? 18:38 - Thank you for watching Namaste JavaScript πŸ™ Support this video series, NOT BY MONEY, but by sharing it with your friends. πŸ™ I'll give my best to come up with great content and everything absolutely for free on YouTube. 😊 If you are active on Social Media, please give a shoutout to Namaste JavaScript and help me reach more people. πŸ™ Cheers, Akshay Saini http://akshaysaini.in Would love to Stay Connected with you ❀️ LinkedIn - https://www.linkedin.com/in/akshaymarch7 Instagram - https://www.instagram.com/akshaymarch7 Twitter - https://twitter.com/akshaymarch7 Facebook - https://www.facebook.com/akshaymarch7 #NamasteJS #AkshaySaini