Challenge - 5 Problems
Function Declaration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of a simple function declaration
What is the output of this JavaScript code?
Javascript
function greet(name) { return `Hello, ${name}!`; } console.log(greet('Alice'));
Attempts:
2 left
π‘ Hint
Look at how the function returns a string using template literals.
β Incorrect
The function greet returns a greeting string with the given name. Calling greet('Alice') returns 'Hello, Alice!'.
β Predict Output
intermediate2:00remaining
Function hoisting behavior
What will this code output when run?
Javascript
console.log(square(4)); function square(x) { return x * x; }
Attempts:
2 left
π‘ Hint
Function declarations are hoisted in JavaScript.
β Incorrect
Function declarations are hoisted, so square is available before its definition. square(4) returns 16.
β Predict Output
advanced2:00remaining
Function declaration inside block scope
What is the output of this code?
Javascript
if (true) { function sayHi() { return 'Hi!'; } } console.log(typeof sayHi);
Attempts:
2 left
π‘ Hint
Function declarations inside blocks are block-scoped in modern JavaScript.
β Incorrect
The function sayHi is block-scoped inside the if block and not available outside, so typeof sayHi is 'undefined'.
π§ Conceptual
advanced2:00remaining
Difference between function declaration and function expression
Which statement correctly describes a key difference between function declarations and function expressions in JavaScript?
Attempts:
2 left
π‘ Hint
Think about when you can call the function in the code.
β Incorrect
Function declarations are hoisted and can be called before their definition. Function expressions are not hoisted.
β Predict Output
expert2:00remaining
Output of nested function declarations and variable shadowing
What is the output of this code?
Javascript
function outer() { function inner() { return 'inner function'; } let inner = 'inner variable'; return inner; } console.log(outer());
Attempts:
2 left
π‘ Hint
Variables declared with let shadow function declarations with the same name.
β Incorrect
The variable inner declared with let shadows the inner function declaration, so outer returns the string 'inner variable'.