LearnFrenzy
Sign in Take a free test

Variables: var, let and const · Exercise 1 of 4

Variables: var, let and const var, let and const MCQ

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Fifteen multiple-choice questions on JavaScript variables: function and block scope, hoisting, the temporal dead zone, redeclaration, const objects, Object.freeze and loop bindings. Pick an option to check your answer instantly.

Question 11 Hard

What happens when console.log(typeof token); runs on the line just before let token = "abc"; in the same scope?

A It prints undefined
B It prints string
C A ReferenceError is thrown
D It prints object
Question page & discussion
Question 12 Medium

Why does for (let i = 0; i < 3; i++) setTimeout(() => console.log(i)); print 0, 1, 2?

A Each iteration gets a new binding of i, which its callback captures
B setTimeout copies the value of i when it is called
C let variables are read-only inside loops
D The loop waits for each timer before continuing
Question page & discussion
Question 13 Medium

In a non-strict script, a function runs discount = 15; and discount was never declared. What happens?

A A global variable (a property of the global object) named discount is created
B A ReferenceError is thrown
C A local variable of the function is created
D The statement is ignored
Question page & discussion
Question 14 Hard

Why does for (const i = 0; i < 3; i++) { … } fail, while for (const x of list) { … } works?

A const cannot be declared inside parentheses
B for…of converts const to let automatically
C Both fail in strict mode only
D i++ tries to reassign the constant i; for…of creates a fresh x for each iteration instead of changing it
Question page & discussion
Question 15 Hard

What happens with let mode = "light"; { var mode = "dark"; } in the same scope?

A SyntaxError: the var is hoisted to the outer scope, where mode is already declared with let
B The block gets its own mode, and the outer one stays "light"
C mode becomes "dark" everywhere
D It works only in strict mode
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links