-1

In JavaScript, typeof {} returns "object", while evaluating {} returns undefined. Why is this?

var a;
a; //undefined
{}; //undefined
typeof a === typeof {}; //false
Michael Jasper
  • 7,962
  • 4
  • 40
  • 60

1 Answers1

5

That's because typeof a is "undefined" and typeof {} is "object":

console.log(typeof a);    // "undefined"
console.log(typeof {});   // "object"

Also, as @adeneo commented, {} is not undefined:

console.log({} == undefined);    // false
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
  • 2
    E.g., the question is based on a misconception: `{}` is not "undefined" in any sense of the word, and certainly not in the technical sense of having the type `"undefined"`. – T.J. Crowder Mar 17 '14 at 15:02