What is the output of the following? “`js let x = 1; function foo(x, y = x * 2) { return y; } console.log(foo(3)); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js let x = 1; function foo(x, y = x * 2) { return y; } console.log(foo(3)); “`

Key points

  • The function `foo` returns the value of `y`
  • The default value of `y` is `x * 2`
  • When `foo(3)` is called, `x` is 3
  • Therefore, `y` is calculated as 3 * 2, resulting in 6

Ready to go further?

Related questions