javascript - typeof Number equals Number - Stack Overflow

In JavaScript,typeof 42 === 'number' trueevaluates to true. But..typeof 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 parison 2 or 3 evaluate to true?

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 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 expect typeof '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
Add a ment  | 

2 Answers 2

Reset to default 4

No, 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 Numberobject. 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

相关推荐

  • javascript - typeof Number equals Number - Stack Overflow

    In JavaScript,typeof 42 === 'number' trueevaluates to true. But..typeof Number

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信