admin管理员组文章数量:1401431
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"
本文标签: javascripttypeof Number equals NumberStack Overflow
版权声明:本文标题:javascript - typeof Number equals Number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744300725a2599569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论