LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Scope and Hoisting MCQ · Question 15

Avoiding globals Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 15 Back to Exercise

Which is the best way to keep helper variables out of the global scope in modern code?

  • A Declare everything with var
  • B Put the code in an ES module (or at least inside a function or block) Correct
  • C Prefix all names with g_
  • D Store them as properties of window
Correct Answer: Option B

Modules give every file its own scope. In older code an IIFE did the same job.

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