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
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.......
@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;
@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]
@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.