0
0
Javascriptprogramming~20 mins

Function expression in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Function Expression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
AReferenceError
B
"Hello!"
"function"
C
"Hello!"
"undefined"
D
"undefined"
"undefined"
Attempts:
2 left
πŸ’‘ Hint
Remember that the function name inside a named function expression is not accessible outside its own scope.
❓ Predict Output
intermediate
1: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));
A12
Bundefined
CNaN
DTypeError
Attempts:
2 left
πŸ’‘ Hint
The function multiplies two numbers and returns the result.
❓ Predict Output
advanced
1:30remaining
Output of a self-invoking function expression
What is the output of this code?
Javascript
console.log((function(x) {
  return x * x;
})(5));
Aundefined
B25
CReferenceError
DTypeError
Attempts:
2 left
πŸ’‘ Hint
This function runs immediately with argument 5.
❓ Predict Output
advanced
2: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());
A
undefined
undefined
B
10
10
C
TypeError
TypeError
D
10
undefined
Attempts:
2 left
πŸ’‘ Hint
Consider what 'this' refers to in each call.
🧠 Conceptual
expert
2:30remaining
Why use function expressions over function declarations?
Which of the following is the main advantage of using function expressions instead of function declarations?
AFunction expressions can be anonymous and assigned to variables, allowing more flexible and dynamic code.
BFunction expressions are hoisted completely, so they can be called before they are defined.
CFunction expressions always run faster than function declarations.
DFunction expressions automatically bind 'this' to the global object.
Attempts:
2 left
πŸ’‘ Hint
Think about how function expressions can be used in different ways compared to declarations.