LearnFrenzy
Sign in Take a free test

JavaScript Basics · Predict the Output · Question 18

Calling a function before its declaration Answer, explanation & discussion

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

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

main.js
greet("Asha");

function greet(name) {
  console.log(`Hello, ${name}`);
}
  • A ReferenceError: greet is not defined
  • B TypeError: greet is not a function
  • C undefined
  • D Hello, Asha Correct
Correct Answer: Option D

Function declarations are hoisted completely – name and body – so they can be called before the line where they appear. (Function expressions assigned to let/const are not.)

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