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'
.
Number
is a function which you can use to wrap a native value into a Number
object. Number
is the also the constructor of the Number
type, if used with new
, eg
new Number(42)
From the documentation :
A Number object is created using the Number() constructor.
So typeof Number
is actually "function"
.
On the other hand, 'number'
is a String, so typeof 'number'
is "string"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.