LearnFrenzy
Sign in Take a free test

JavaScript Basics · Predict the Output · Question 14

Assigning a variable that was never declared Answer, explanation & discussion

  • Output
  • Medium
  • 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
function setScore() {
  score = 90;        // no let, const or var
}
setScore();
console.log(score);
  • A ReferenceError: score is not defined
  • B undefined
  • C 90 Correct
  • D SyntaxError
Correct Answer: Option C

In non-strict code, assigning to an undeclared name creates a property on the global object, so score is visible outside the function. In strict mode the same code throws ReferenceError.

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