LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 4

typeof before a var and a function of the same name Answer, explanation & discussion

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

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

main.js
function f() {
  return typeof g;
  var g = 5;
  function g() {}
}
console.log(f());
  • A number
  • B undefined
  • C function Correct
  • D ReferenceError
Correct Answer: Option C

During creation, the function declaration g takes precedence over the var. The return runs before the assignment g = 5 could ever happen.

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