Challenge - 5 Problems
Function Expression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of a named function expression
What is the output of this code?
Javascript
const greet = function sayHello() { return 'Hello!'; }; console.log(greet()); console.log(typeof sayHello);
Attempts:
2 left
π‘ Hint
Remember that the function name inside a named function expression is not accessible outside its own scope.
β Incorrect
The function expression assigns a function named sayHello to the variable greet. Calling greet() returns 'Hello!'. However, sayHello is not defined in the outer scope, so typeof sayHello returns 'undefined'.
β Predict Output
intermediate1:30remaining
Output of an anonymous function expression assigned to a variable
What will this code print?
Javascript
const multiply = function(a, b) { return a * b; }; console.log(multiply(3, 4));
Attempts:
2 left
π‘ Hint
The function multiplies two numbers and returns the result.
β Incorrect
The anonymous function takes two arguments and returns their product. Calling multiply(3, 4) returns 12.
β Predict Output
advanced1:30remaining
Output of a self-invoking function expression
What is the output of this code?
Javascript
console.log((function(x) { return x * x; })(5));
Attempts:
2 left
π‘ Hint
This function runs immediately with argument 5.
β Incorrect
The function is immediately invoked with 5, returning 5 * 5 = 25.
β Predict Output
advanced2:00remaining
Output of a function expression inside an object
What will this code output?
Javascript
const obj = { value: 10, getValue: function() { return this.value; } }; const getValue = obj.getValue; console.log(obj.getValue()); console.log(getValue());
Attempts:
2 left
π‘ Hint
Consider what 'this' refers to in each call.
β Incorrect
obj.getValue() is called as a method, so 'this' refers to obj and returns 10. getValue() is called as a standalone function, so 'this' is undefined in strict mode, returning undefined.
π§ Conceptual
expert2:30remaining
Why use function expressions over function declarations?
Which of the following is the main advantage of using function expressions instead of function declarations?
Attempts:
2 left
π‘ Hint
Think about how function expressions can be used in different ways compared to declarations.
β Incorrect
Function expressions can be anonymous and assigned to variables or passed as arguments, enabling flexible and dynamic programming patterns. Function declarations are hoisted, but expressions are not. Performance and 'this' binding are not guaranteed advantages.