0
0
Javascriptprogramming~5 mins

Function expression in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a function expression in JavaScript?
A function expression is a way to define a function inside an expression, often assigned to a variable. It can be anonymous or named and is not hoisted like function declarations.
Click to reveal answer
beginner
How do you assign a function expression to a variable?
You write: <code>const myFunc = function() { /* code */ };</code> This stores the function in the variable <code>myFunc</code>.
Click to reveal answer
intermediate
What is the difference between a function declaration and a function expression?
Function declarations are hoisted, so you can call them before they appear in code. Function expressions are not hoisted, so you must define them before calling.
Click to reveal answer
beginner
Can function expressions be anonymous? What does that mean?
Yes, function expressions can be anonymous, meaning they have no name. For example: <code>const f = function() { }</code>. The function has no name but is stored in <code>f</code>.
Click to reveal answer
intermediate
How do arrow functions relate to function expressions?
Arrow functions are a shorter syntax for writing function expressions. For example: <code>const add = (a, b) => a + b;</code> is a function expression using arrow syntax.
Click to reveal answer
Which of these is a valid function expression?
Aconst greet = function() { console.log('Hi'); };
Bfunction greet() { console.log('Hi'); }
Cgreet() => { console.log('Hi'); }
Dfunction: greet() { console.log('Hi'); }
What happens if you try to call a function expression before it is defined?
AIt works fine because functions are hoisted.
BIt calls the function but with empty parameters.
CIt returns undefined.
DIt causes an error because function expressions are not hoisted.
Which is true about anonymous function expressions?
AThey cannot be assigned to variables.
BThey have no name and are often assigned to variables.
CThey must have a name.
DThey are hoisted like function declarations.
How do arrow functions differ from regular function expressions?
AArrow functions must have names.
BArrow functions are hoisted, regular ones are not.
CArrow functions have a shorter syntax and do not have their own 'this'.
DArrow functions cannot be assigned to variables.
Which keyword is used to assign a function expression to a variable?
Aconst or let
Bvar only
Cfunction
Ddef
Explain what a function expression is and how it differs from a function declaration.
Think about when you can call the function in your code.
You got /3 concepts.
    Describe how arrow functions relate to function expressions and mention one key difference.
    Consider syntax and behavior differences.
    You got /3 concepts.