/    Sign up×
Community /Pin to ProfileBookmark

simple addition in Javascript

funniest thing. I have done addition in JS many times. Now for some reason when I am adding two variables (both contain numbers say 2000 and 90) the result is a concatenation of the two “200090” not 2090 as one would think. What simple mistake am I making? Thanks:o

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Warren86Dec 03.2004 — Your operands are strings, not numbers.


isA = "2000"

isB = "90"

isSum = parseInt(isA)+parseInt(is?;

See, also parseFloat(), for real numbers. parseInt is for integers.

See, also, toFixed(n), to fix the number of decimal positions.
Copy linkTweet thisAlerts:
@JSMarlowauthorDec 03.2004 — Warren:

Thanks for the response. This was my first thought so I set up these variables as numeric with isA = 0; and isB = 0; and they still seemed to concatenate. ummm.......
Copy linkTweet thisAlerts:
@Warren86Dec 03.2004 — That's probably because you didn't the define the variable:

isA = new Number()

isA = 2000
Copy linkTweet thisAlerts:
@JSMarlowauthorDec 03.2004 — Warren:

I thought this is what I did with isA = 0; JS is not typed so where else can I do?

Also, I am not sure what you mean with: isA = new Number()

Thanks again.

Scott
Copy linkTweet thisAlerts:
@TheBearMayDec 03.2004 — "isA = new Number()" just declares a variable isA as type Number.

Something else to look at - if you're using the variables in a string operation (ie, outputting a message) either do the math in parens or do it first and then do the string operation:

var msgStr = "The value of A + B is " + (isA+is?;

instead of

var msgStr = "The value of A + B is " + isA + isB;
Copy linkTweet thisAlerts:
@Warren86Dec 03.2004 — Well, as far as I know, concerning math, JS is typed. You attempted to perform addition on "string" types, and the result is a concatation. The is new Number(), TYPES that variable to a numeric type. It can also be used: isA = new Number(2000);


I'm attaching a .txt file, I found useful for JS math, maybe you will too.

Regarding your original question, it will be necessary to convert those strings to numbers prior to performing math addition with them, using parseInt().

[upl-file uuid=fd1ce06f-52b1-4712-87c5-d7129d33e2b7 size=20kB]javascript math faqs.txt[/upl-file]
Copy linkTweet thisAlerts:
@JSMarlowauthorDec 03.2004 — OK, thanks to all. I will read up on math operations as it appears that I did not know about all the tools available to me.

Scott.
×

Success!

Help @JSMarlow spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...