Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "var n=Number(3);" work?

Tags:

javascript

I used var n=Number(3);by mistake (I should have usedvar n=new Number(3);), but I got n=3. As Number() is an object constructor, who can explain this?

like image 791
SPiCa Avatar asked Feb 27 '14 03:02

SPiCa


1 Answers

An object constructor is also a function.

Number(MDN doc) as a function can be used to convert to a primitive type number.

> Number(3)
  3
> Number("3")
  3
> Number("A")
  NaN
> Number("2e2")
  200
> Number("0xff")
  255
> ["1", "2", "3"].map(Number)
  [1, 2, 3]
like image 64
xdazz Avatar answered Sep 28 '22 03:09

xdazz