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 parison 2 or 3 evaluate to true?
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 parison 2 or 3 evaluate to true?
Share Improve this question edited Jan 13, 2015 at 20:15 JLRishe 102k19 gold badges137 silver badges171 bronze badges asked Jan 13, 2015 at 19:28 FungyBytesFungyBytes 4131 gold badge6 silver badges14 bronze badges 3-
1
Why do you expect
typeof 'number'
to return'number'
? What do you expecttypeof 'foo'
to return? – Felix Kling Commented Jan 13, 2015 at 19:40 - I see now. typeof 'foo' returns 'string'. I guess I was confused because the return value of typeof is quoted. – FungyBytes Commented Jan 13, 2015 at 19:45
-
1
typeof
always returns a string. – Felix Kling Commented Jan 13, 2015 at 19:49
2 Answers
Reset to default 4No, Number
, String
, and Boolean
are all objects (and functions). typeof
applied to any of them will return the value "function"
.
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
https://developer.mozilla/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
, e.g.
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"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744300726a4567488.html
评论列表(0条)