admin管理员组

文章数量:1401431

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"

本文标签: javascripttypeof Number equals NumberStack Overflow