Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

Why doesn't typeof throw for undeclared variables?

Do you know what will be the output of this code?

JavaScript
console.log(typeof notDeclared);
Show answer and explanation

The answer is "undefined".

typeof is the one operator that is safe to use on undeclared identifiers. It was designed that way for feature detection, like typeof window !== "undefined".

It does throw inside a temporal dead zone (let before its declaration).

Take the full quiz Next question →

More Scope & Hoisting questions

  1. Is a var loop variable accessible after a for loop?
  2. Why does var in a loop with setTimeout print 3 3 3?
  3. Why does let in a for loop fix the setTimeout closure problem?
  4. Why does using a var before its declaration return undefined?
  5. What is the temporal dead zone in JavaScript?

Related topics