LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Predict the Output · Question 12

A returned function and two variables called prefix Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

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

main.js
function makeLogger() {
  const prefix = "[LF]";
  return (msg) => `${prefix} ${msg}`;
}
const prefix = "[OTHER]";
console.log(makeLogger()("ready"));
  • A [OTHER] ready
  • B undefined ready
  • C ReferenceError
  • D [LF] ready Correct
Correct Answer: Option D

The arrow function was created inside makeLogger, so its nearest prefix is makeLogger's. The outer prefix is never consulted.

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