0
0
Javascriptprogramming~20 mins

Function parameters in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Function Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of function with default parameters
What is the output of the following JavaScript code?
Javascript
function greet(name = "Guest", greeting = "Hello") {
  return `${greeting}, ${name}!`;
}

console.log(greet(undefined, "Hi"));
A"Hi, Guest!"
B"Hi, undefined!"
C"Hello, Guest!"
D"Hello, undefined!"
Attempts:
2 left
πŸ’‘ Hint
Remember that passing undefined triggers default parameters.
❓ Predict Output
intermediate
2:00remaining
Rest parameters and arguments length
What will be logged to the console when this code runs?
Javascript
function sum(...numbers) {
  return numbers.reduce((a, b) => a + b, 0);
}

console.log(sum(1, 2, 3, 4));
Aundefined
B1234
C10
DTypeError
Attempts:
2 left
πŸ’‘ Hint
Rest parameters collect all arguments into an array.
❓ Predict Output
advanced
2:00remaining
Parameter destructuring with default values
What is the output of this code snippet?
Javascript
function display({name = "Unknown", age = 0} = {}) {
  console.log(`${name} is ${age} years old.`);
}

display({age: 25});
ATypeError
B"Unknown is 25 years old."
C"Unknown is undefined years old."
D"undefined is 25 years old."
Attempts:
2 left
πŸ’‘ Hint
Missing properties use default values in destructuring.
❓ Predict Output
advanced
2:00remaining
Effect of parameter order with default and rest
What will this code output?
Javascript
function test(a, b = 2, ...rest) {
  console.log(a, b, rest.length);
}

test(1, undefined, 3, 4);
A1 3 1
B1 undefined 2
C1 2 0
D1 2 2
Attempts:
2 left
πŸ’‘ Hint
Undefined triggers default parameter; rest collects remaining arguments.
🧠 Conceptual
expert
3:00remaining
Understanding parameter shadowing and closures
Consider the following code. What is the value of variable 'result' after execution?
Javascript
let result;
function outer(x) {
  return function inner(x = 5) {
    result = x;
  };
}

const fn = outer(10);
fn();
A5
B10
Cundefined
DReferenceError
Attempts:
2 left
πŸ’‘ Hint
Inner function parameter shadows outer parameter; default applies when no argument passed.