Understanding Function Hoisting Behavior
π Scenario: Imagine you are writing a small JavaScript program where you want to understand how functions behave when called before they are written in the code. This is important because sometimes JavaScript lets you call functions before their actual code appears, and sometimes it does not.
π― Goal: You will create two functions: one declared with the function keyword and one assigned to a variable using const. You will then try calling both functions before and after their definitions to see which one works and which one causes an error.
π What You'll Learn
Create a function declaration named
greet that returns the string 'Hello from greet!'.Create a function expression assigned to a constant named
sayBye that returns the string 'Goodbye from sayBye!'.Call the
greet function before its declaration and store the result in a variable named beforeGreet.Call the
sayBye function before its assignment and store the result in a variable named beforeSayBye.Call both functions after their definitions and store the results in variables named
afterGreet and afterSayBye respectively.Print all four variables to observe the behavior.
π‘ Why This Matters
π Real World
Knowing function hoisting helps when reading or writing JavaScript code that calls functions in different orders, especially in larger projects or when using libraries.
πΌ Career
Many JavaScript developer jobs require understanding hoisting to debug code and write clean, error-free functions.
Progress0 / 4 steps