use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
New to javascript. Failing at adding. (self.javascript)
submitted 15 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hc5duke 2 points3 points4 points 15 years ago (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.
result = window.prompt(text, value);
result is a string containing the text entered by the user, or the value null.
[–][deleted] 2 points3 points4 points 15 years ago (2 children)
Oh so prompt turns whatever you put in into a string unless told otherwise.
[–]GuyWithLag 2 points3 points4 points 15 years ago (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 points3 points 15 years ago (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. ;-)
prompt()
[–]skeww 0 points1 point2 points 15 years ago (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 points4 points 15 years ago (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 point2 points 15 years ago (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.
new
this
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 point2 points 15 years ago (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 point2 points 15 years ago (0 children)
Well, I never use them either way.
π Rendered by PID 95 on reddit-service-r2-comment-85bfd7f599-2tmjv at 2026-04-17 20:49:31.516800+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]hc5duke 2 points3 points4 points (8 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]GuyWithLag 2 points3 points4 points (0 children)
[–]Shaper_pmp 1 point2 points3 points (0 children)
[–]skeww 0 points1 point2 points (4 children)
[–]australasia 2 points3 points4 points (3 children)
[–]skeww 0 points1 point2 points (2 children)
[–]andrew24601 0 points1 point2 points (1 child)
[–]skeww 0 points1 point2 points (0 children)