In JavaScript,
typeof 42 === 'number' //true
evaluates to true. But..
typeof Number === 'number' //false
evalutes to false. And..
typeof 'number' === 'number' //false
also evaluates to false.
Shouldn't comparison 2 or 3 evaluate to true?
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'
.