开发者

Curiosity Encountered in Javascript Tutorial

开发者 https://www.devze.com 2023-01-22 17:34 出处:网络
I am not understanding what the Number function does. This is the tutorial I\'m using, which in fact does explain what the function does... BUT... when I delete Number it still returns the same开发者_

I am not understanding what the Number function does. This is the tutorial I'm using, which in fact does explain what the function does... BUT... when I delete Number it still returns the same开发者_JAVA技巧 value, so I'm not understanding its purpose. Even if I change Number to String it still returns the same value. Here is the example code:

var theNumber = Number(prompt("Pick a number", ""));  
print("Your number is the square root of " +  
    (theNumber * theNumber));


Whats happening is its automatically converting theNumber to a number to do the *.

I am assuming you are saying you did:

var theNumber = prompt("Pick a number", "");  
print("Your number is the square root of " +  
(theNumber * theNumber));

Here is a good reference that explains "Automatic Conversions"


It looks like you have a second question, and is "Why would I ever use it?"

Once you have convert it you a "Number" object you now have access to some additional properties/methods which allow you to do some conversions like adding/trimming decimals, as well as converting to Exponential Notation.

Example (user Enters 1.22222222222222222222222222):

var theNumber = new Number("1.22222222222222222222222222");
theNumber.toFixed(2);
//value is now 1.22
0

精彩评论

暂无评论...
验证码 换一张
取 消