0

When I do: var answer=a+b

if a was 4 and b was 5 then my answer comes out as 45. How can i get them to numerically add. I can do all other operations(*/-) but not add. Ik its a stupid question but im new and trying to lean

var prea,a,answer2,answer4,answer3,b,preb,answer1;

prea=document.getElementById("form1") ;
a=prea.elements["first"].value;
preb=document.getElementById("form1") ;
b=preb.elements["second"].value;
answer1=a*b;
answer2=a-b;
answer3=a/b;
answer4=a+b;
document.write("Multiplication:"+answer1); 
document.write("<br>");
document.write("Subtraction:"+answer2);
document.write("<br>");
document.write("Division:"+answer3);
document.write("<br>");
document.write("Add:"+answer4);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • parseInt :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt – Babak Bandpey Nov 24 '17 at 23:07
  • 1
    You're surely operating on text input fields; any text input field's value is [a string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String). Because you need [a number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) instead, try to cast a string to a number: `Number(a) + Number(b)`. – rishat Nov 24 '17 at 23:07
  • Welcome to the _wonderful_ world of javascript :) – Sergio Tulentsev Nov 24 '17 at 23:07
  • Also, don’t use `document.write`! – Sebastian Simon Nov 24 '17 at 23:09
  • 2
    Possible duplicate of [How to force addition instead of concatenation in javascript](https://stackoverflow.com/questions/13953939/how-to-force-addition-instead-of-concatenation-in-javascript) – Sebastian Simon Nov 24 '17 at 23:11

2 Answers2

1

Use Number(a) + Number(b) to calculate them. If you using strings instead of numbers, you just concatinate them instead of adding.

https://www.w3schools.com/jsref/jsref_number.asp

Tyr
  • 2,810
  • 1
  • 12
  • 21
  • Why are `a` and `b` strings? Why do the "other operations" work? – Andreas Nov 24 '17 at 23:10
  • If you are using other operators like *, / or -, there is an auto conversion from string operands to numbers. It just works, because these operators don't do anything else. The + operator instead is used for concatination and also for addition and since javascript isn't well developed, the + operator is used for concatination per default. – Tyr Nov 24 '17 at 23:18
  • @Tyr “auto conversion” is commonly referred to as “coercion”. JavaScript isn’t “not well developed” just because `+` has multiple semantics. – Sebastian Simon Nov 24 '17 at 23:23
  • 1
    This (without the opinion-based part) should be part of your answer and not buried in a comment. – Andreas Nov 24 '17 at 23:23
0

The variables are being declared as strings. You need to type cast them as an integer value to properly add them using parseInt(string).

The reason all the other operators work is because they will try to type juggle. However, javascript uses + for both numerical addition and string concatenation. So you have to explicitly use integer types if you want the result to be a summation.

cchoe1
  • 325
  • 2
  • 15
  • 1
    [MDN: Dynamic typing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures) _"JavaScript is a loosely typed or a dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types"_ – Andreas Nov 24 '17 at 23:13