Recall & Review
beginner
What is function hoisting in JavaScript?
Function hoisting means JavaScript moves function declarations to the top of their scope before code runs, so you can call functions before they appear in the code.
Click to reveal answer
intermediate
How does hoisting differ between function declarations and function expressions?
Function declarations are fully hoisted, so you can call them before they appear. Function expressions are not hoisted the same way; if assigned to a variable, only the variable declaration is hoisted, not the assignment.
Click to reveal answer
beginner
What happens if you call a function expression before it is defined?
If you call a function expression before it is assigned, you get a TypeError because the variable is hoisted but undefined at that point.
Click to reveal answer
beginner
Example: What will this code output?
<pre>console.log(greet());
function greet() { return 'Hello!'; }</pre>It will output 'Hello!' because the function declaration 'greet' is hoisted, so it exists before the console.log runs.
Click to reveal answer
intermediate
Example: What will this code output?
console.log(sayHi());
var sayHi = function() { return 'Hi!'; };It will cause a TypeError because 'sayHi' is hoisted as a variable but is undefined when called, so you cannot call it as a function yet.
Click to reveal answer
What does function hoisting allow you to do in JavaScript?
✗ Incorrect
Function hoisting moves function declarations to the top, so you can call them before their code appears.
Which of these is fully hoisted in JavaScript?
✗ Incorrect
Function declarations are fully hoisted with their body; function expressions and arrow functions are not.
What error occurs if you call a function expression before it is assigned?
✗ Incorrect
Calling an undefined variable as a function causes a TypeError.
Given this code, what will happen?
console.log(foo());
var foo = function() { return 'bar'; };✗ Incorrect
Variable 'foo' is hoisted but undefined at the call, so calling it causes a TypeError.
Which statement about function hoisting is true?
✗ Incorrect
Only function declarations are fully hoisted; function expressions are not.
Explain function hoisting and how it affects calling functions before their definition.
Think about what JavaScript does before running your code.
You got /4 concepts.
Describe the difference in hoisting behavior between a function declaration and a function expression assigned to a variable.
Consider what happens to the variable and the function code separately.
You got /4 concepts.