0
0
Javascriptprogramming~5 mins

Function hoisting behavior in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
APrevent functions from being overwritten
BUse variables before declaring them
CCall functions before they are declared
DAutomatically run functions on page load
Which of these is fully hoisted in JavaScript?
AFunction declaration
BFunction expression assigned to var
CArrow function assigned to let
DClass declaration
What error occurs if you call a function expression before it is assigned?
AReferenceError
BTypeError
CSyntaxError
DNo error
Given this code, what will happen?
console.log(foo());
var foo = function() { return 'bar'; };
A'bar' is printed
Bundefined is printed
CReferenceError: foo is not defined
DTypeError: foo is not a function
Which statement about function hoisting is true?
AOnly function declarations are hoisted, not function expressions
BBoth function declarations and expressions are fully hoisted
CFunction expressions are hoisted before declarations
DHoisting only applies to variables
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.