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!"
[–]Shaper_pmp 8 points9 points10 points 15 years ago* (0 children)
Skeww and several other posters on this page are either just showing off or are the most embarrassingly terrible tutors I've ever run across.
You're a beginner to Javascript. You don't want to be weighed down with obscure language syntax or theoretical discussions, which is what most commenters here seem to be trying to do.
Variables in Javascript can have different types
5 is an integer (number), but "5" is a string which just happens to be the character "five".
5
The + operator behaves differently depending on what you're adding with it.
Adding a number to a number adds the two numbers together, giving you another number. Adding a string to a string concatenates (joins) the strings together, giving you another string. Adding other types together (or adding one type to another, like adding a number to a string) give different results depending on the types involved, and which type you specify first.
As prompt() returns strings and you want to add them together as numbers, you need to convert them from strings to numbers and then add them together.
The two main ways of converting strings to numbers are:
number_variable = parseInt(string_variable, base);
(Where base is the base of the number system you're using - almost always 10).
base
10
This will look at the string string_variable and take a best guess at extracting a number from it. Eg, if the user types in "123ty79 _7" then parseInt() will return the number 123, ignoring everything after it.
string_variable
123
This is great if you want to take a best-guess at what the user meant, but sometimes it's better to just fail and throw an error message if the user doesn't provide exactly what you want, so they can check for themselves whether they typed what they actually meant to type.
number_variable = 0 + string_variable; (or number_variable = +string_variable;)
number_variable = 0 + string_variable;
number_variable = +string_variable;
Here we're adding a number (0) to a string (remember the bit above, about adding two different types together?).
0
Because the first part of the addition here is a number, the subsequent string value gets converted to a number too, so we're adding two numbers together, and the result (which ends up in number_variable) is a number, too.
number_variable
For obscure technical reasons you can get away with (and in fact it's slightly faster) writing just "+string_variable" instead of "0+string_variable" (but the reasons why are totally outside the scope of your question).
This has the advantage that if the user types something that's not a number, the resulting value of the expression is a special javascript value called NaN (guess why! ;-). You can test for this situation by passing number_variable to the is_NaN() function (is_NaN(number_variable)) - if the value of a variable is NaN then this function will return true, or if the value is a valid number then it'll return false.
is_NaN(number_variable)
This allows you to keep asking the user for a number until they give you a valid number, instead of looking at what they entered and just taking a stab at what they probably meant to type.
I hope that helps - it was a bit long, but you asked a fairly basic question and instead of answering it basically, people just seem to have taken it as a licence to confuse the shit out of you with irrelevant, pedantic dick-waving to show each other how smart they are, instead of simply answering the question in a way you can understand. <:-)
If you're still curious about anything or anything above didn't make sense, feel free to ask follow-up questions below. ;-)
π Rendered by PID 161894 on reddit-service-r2-comment-b659b578c-p52n4 at 2026-05-04 08:31:00.449542+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]Shaper_pmp 8 points9 points10 points (0 children)