Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

When do default parameters apply in JavaScript?

Do you know what will be the output of this code?

JavaScript
function f(x = 10) {
  return x;
}

console.log(f(undefined), f(null));
Show answer and explanation

The answer is 10 null.

A default value is used only when the argument is missing or undefined. null is a real value, so it’s passed through.

Take the full quiz Next question →

More Functions questions

  1. How do closures create private variables in JavaScript?
  2. How do you write a once() function with a closure?
  3. Can you call a function before it is declared in JavaScript?
  4. Why can't you call a function expression before it's defined?
  5. Can you change this on a bound function with call()?

Related topics