Search code examples
javascriptnumberstypeof

typeof Number equals Number


In JavaScript,

  1. typeof 42 === 'number' //true

evaluates to true. But..

  1. typeof Number === 'number' //false

evalutes to false. And..

  1. typeof 'number' === 'number' //false

also evaluates to false.

Shouldn't comparison 2 or 3 evaluate to true?


Solution

  • No, Number, String, and Boolean are all objects (and functions). typeof applied to any of them will return the value "function".

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean


    The value 'number' is a string and therefore its type is 'string'.