Challenge - 5 Problems
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
What is the output of this function call?
Consider the following JavaScript function. What will be logged to the console when
foo() is called?Javascript
function foo() { return 5 + 3; } console.log(foo());
Attempts:
2 left
π‘ Hint
Remember that the
return statement sends a value back to where the function was called.β Incorrect
The function
foo returns the sum of 5 and 3, which is 8. The console.log prints this returned value.β Predict Output
intermediate2:00remaining
What does this function return?
What is the value of
result after calling bar()?Javascript
function bar() { let x = 10; if (x > 5) { return x * 2; } return x / 2; } const result = bar();
Attempts:
2 left
π‘ Hint
Check the condition inside the function and which return statement runs first.
β Incorrect
Since x is 10 which is greater than 5, the function returns 10 * 2 = 20 immediately.
β Predict Output
advanced2:00remaining
What is the output of this nested function call?
What will be printed when this code runs?
Javascript
function outer() { function inner() { return 'Hello'; } return inner(); } console.log(outer());
Attempts:
2 left
π‘ Hint
The outer function calls the inner function and returns its result.
β Incorrect
The inner function returns 'Hello', and outer returns the result of inner(), so console.log prints 'Hello'.
β Predict Output
advanced2:00remaining
What does this function return when no explicit return is given?
What is the output of this code?
Javascript
function noReturn() { let a = 5; let b = 10; let c = a + b; } console.log(noReturn());
Attempts:
2 left
π‘ Hint
If a function does not return a value explicitly, what does it return by default?
β Incorrect
Functions without a return statement return undefined by default in JavaScript.
β Predict Output
expert2:00remaining
What is the output of this asynchronous function?
What will be logged to the console when this code runs?
Javascript
async function asyncFunc() { return 'Done'; } asyncFunc().then(console.log);
Attempts:
2 left
π‘ Hint
Remember that async functions return a Promise, but using .then() logs the resolved value.
β Incorrect
The async function returns a Promise that resolves to 'Done'. The .then() logs the resolved value 'Done'.