#number plus #number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

#number plus #number

I would appreciate if somebody tell me why two numbers concatenate (just like two string ) when I want to add them together in functions. function Add(){ var a=document.getElementById(“id..”).value; var b=document.getElementById(“id..”).value; var aB= a+b(ex:5+5); document.write(aB);} instead of 10 it gives me 55 Moreover other operators like multiple divisions subtract works well.

24th Feb 2018, 3:44 PM
Roohollah Habibi
Roohollah Habibi - avatar
2 Answers
+ 5
Well, this is happening because when the .value method is used, it returns whatever value within the tag specified as a string, which is why the two values concatenate. To set the variables to integers, you can surround the document.getEle... in a parseInt() function, which takes a string and returns an integer from it. For example: var a = parseInt(document.getElementById("ex").value); Hope this helped!
24th Feb 2018, 3:52 PM
Faisal
Faisal - avatar
+ 3
i think you gonna transform them witch parseInt when you got prompt, language think that's string
24th Feb 2018, 3:56 PM
Mouhamed Gassama
Mouhamed Gassama - avatar