LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 14

A default parameter and a body variable Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 14 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const x = "outer";
function f(a = x) {
  const x = "inner";
  return a;
}
console.log(f());
  • A inner
  • B ReferenceError
  • C outer Correct
  • D undefined
Correct Answer: Option C

Defaults are evaluated in the parameter scope, which sits outside the function body. The body's x is not visible there, so a takes the outer x.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links