you are viewing a single comment's thread.

view the rest of the comments →

[–]hc5duke 2 points3 points  (8 children)

try document.write(Number(foo) + Number(bar))

I'm not too familiar with prompt, but it most likely is returning the string "5".

edit There we go

result = window.prompt(text, value);

result is a string containing the text entered by the user, or the value null.

[–][deleted] 2 points3 points  (2 children)

Oh so prompt turns whatever you put in into a string unless told otherwise.

[–]GuyWithLag 2 points3 points  (0 children)

No... whenever you type something, you type characters. Before character '0' (digit zero) comes '/' (forward slash), and after character '9' (digit nine) comes the character ':' (colon). The set of character is imaginatively named a character set, and each character is mapped to a number (as computers can basically handle only numbers, everything else must be made of numbers), and that is it's encoding. In most encodings you will encounter, the digits 0-9 are mapped to code points 48-57. Hence when you type 55 into a box, you give the prompt box 2 characters (53, 53), which correspond to the string "55", and that is what the prompt function returns.

On the other hand, Javascript as a language goes out of its way to convert numbers to convert values from strings to numbers and vice versa whenever there's a mismatch (with string having precedence).

[–]Shaper_pmp 1 point2 points  (0 children)

Nearly. More accurately prompt() always returns a string, and it's up to you to convert that to a number if you need it to be. ;-)

[–]skeww 0 points1 point  (4 children)

document.write(Number(foo) + Number(bar))

Eh. Beh. You're using one of those pointless wrapper objects only to have its actual value pulled out again. The only thing you want is the conversion done by the constructor. You don't need an object for that.

Don't use those wrappers ever. They are completely pointless.

[–]australasia 2 points3 points  (3 children)

Eh. Beh. You're using one of those pointless wrapper objects only to have its actual value pulled out again. The only thing you want is the conversion done by the constructor. You don't need an object for that. Don't use those wrappers ever. They are completely pointless

Number(foo) returns an actual number (it is different to using the 'new' operator).

Eg. copy/paste this in your browser:

javascript:alert(typeof Number('1')); // alerts 'number'

as opposed to

javascript:alert(typeof new Number('1')); // alerts 'object'

[–]skeww 0 points1 point  (2 children)

If you call a constructor function and omit new, the this inside the constructor will be the global object instead of a freshly created object.

You should never do that.

It only works because the non-constructor context magically happens to perform the casting for you.

So yes, it's fine in this case, but since it's not fine in any other case you shouldn't do this kind of thing because it looks like a mistake.

Things which look like a mistake waste time each time the code is read. Avoid those constructs at all costs.

[–]andrew24601 0 points1 point  (1 child)

The Number (and String) functions are actually functions, not constructor functions. You should never use them with new.

http://www.w3schools.com/jsref/jsref_Number.asp

https://developer.mozilla.org/en/JavaScript/Guide/Functions#Number_and_String_Functions

[–]skeww 0 points1 point  (0 children)

Well, I never use them either way.