How can I add numbers instead of combining them in a string? [duplicate]
This question already has an answer here:
Adding two numbers concatenates them instead of calculating the sum
20 answers
If I use this code it makes from an number an string but I can't figure out why.
I want that this program adds 8+9 and makes = 17 not 89.
I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
javascript
marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Adding two numbers concatenates them instead of calculating the sum
20 answers
If I use this code it makes from an number an string but I can't figure out why.
I want that this program adds 8+9 and makes = 17 not 89.
I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
javascript
marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Change your strings to numbersparseInt(a) + parseInt(b)
should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Ariel Alvarado
Nov 13 '18 at 22:46
add a comment |
This question already has an answer here:
Adding two numbers concatenates them instead of calculating the sum
20 answers
If I use this code it makes from an number an string but I can't figure out why.
I want that this program adds 8+9 and makes = 17 not 89.
I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
javascript
This question already has an answer here:
Adding two numbers concatenates them instead of calculating the sum
20 answers
If I use this code it makes from an number an string but I can't figure out why.
I want that this program adds 8+9 and makes = 17 not 89.
I try to learn this for as a hobby but I tried this almost a year ago and when I got stuck I never tried to make it work till now.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
This question already has an answer here:
Adding two numbers concatenates them instead of calculating the sum
20 answers
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = a + b;
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
javascript
javascript
edited Nov 14 '18 at 3:05
isherwood
37k1082111
37k1082111
asked Nov 13 '18 at 22:43
GiovanniGiovanni
165
165
marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by isherwood, Graham, stealthyninja, Vivek Mishra, Gerhard Barnard Nov 14 '18 at 7:03
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Change your strings to numbersparseInt(a) + parseInt(b)
should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Ariel Alvarado
Nov 13 '18 at 22:46
add a comment |
2
Change your strings to numbersparseInt(a) + parseInt(b)
should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Ariel Alvarado
Nov 13 '18 at 22:46
2
2
Change your strings to numbers
parseInt(a) + parseInt(b)
should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…– Ariel Alvarado
Nov 13 '18 at 22:46
Change your strings to numbers
parseInt(a) + parseInt(b)
should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…– Ariel Alvarado
Nov 13 '18 at 22:46
add a comment |
3 Answers
3
active
oldest
votes
document.getElementById("uitkomst1").innerHTML
returns the string, so your result will be a string of two strings added together.
You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.
In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.
var numberA = new Number(a); // "8.1"
var numberB = new Number(b); // "9.1"
var total = numberA + numberA ; // 17.2
add a comment |
In JavaScript, you can use parseInt(string)
to do that. Like,
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = parseInt(a) + parseInt(b);
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
Make sure user input don't have any decimals, else it will removed inparseInt
– kiranvj
Nov 14 '18 at 3:16
add a comment |
There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.
I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though
var total = a*1 + b*1;
var a = "1";
var b = "12.5";
var total = a*1 + b*1;
console.log(total)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
document.getElementById("uitkomst1").innerHTML
returns the string, so your result will be a string of two strings added together.
You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.
In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.
var numberA = new Number(a); // "8.1"
var numberB = new Number(b); // "9.1"
var total = numberA + numberA ; // 17.2
add a comment |
document.getElementById("uitkomst1").innerHTML
returns the string, so your result will be a string of two strings added together.
You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.
In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.
var numberA = new Number(a); // "8.1"
var numberB = new Number(b); // "9.1"
var total = numberA + numberA ; // 17.2
add a comment |
document.getElementById("uitkomst1").innerHTML
returns the string, so your result will be a string of two strings added together.
You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.
In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.
var numberA = new Number(a); // "8.1"
var numberB = new Number(b); // "9.1"
var total = numberA + numberA ; // 17.2
document.getElementById("uitkomst1").innerHTML
returns the string, so your result will be a string of two strings added together.
You need to convert the string to a number if you want to add two numbers. Javascript has different ways to do this.
In your case, it should use the Number class to convert. I think it is the best option because it can convert decimal numbers too.
var numberA = new Number(a); // "8.1"
var numberB = new Number(b); // "9.1"
var total = numberA + numberA ; // 17.2
edited Nov 14 '18 at 4:18
Mihai Chelaru
2,210101122
2,210101122
answered Nov 14 '18 at 3:00
BinBin
262
262
add a comment |
add a comment |
In JavaScript, you can use parseInt(string)
to do that. Like,
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = parseInt(a) + parseInt(b);
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
Make sure user input don't have any decimals, else it will removed inparseInt
– kiranvj
Nov 14 '18 at 3:16
add a comment |
In JavaScript, you can use parseInt(string)
to do that. Like,
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = parseInt(a) + parseInt(b);
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
Make sure user input don't have any decimals, else it will removed inparseInt
– kiranvj
Nov 14 '18 at 3:16
add a comment |
In JavaScript, you can use parseInt(string)
to do that. Like,
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = parseInt(a) + parseInt(b);
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
In JavaScript, you can use parseInt(string)
to do that. Like,
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = parseInt(a) + parseInt(b);
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = parseInt(a) + parseInt(b);
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo_totaal"></p>
<p id="uitkomst1">8</p>
<p id="uitkomst2">9</p>
<script>
var a = document.getElementById("uitkomst1").innerHTML;
var b = document.getElementById("uitkomst2").innerHTML;
var total = parseInt(a) + parseInt(b);
document.getElementById("demo_totaal").innerHTML = total;
</script>
</body>
</html>
answered Nov 13 '18 at 22:47
Elliott FrischElliott Frisch
154k1391181
154k1391181
Make sure user input don't have any decimals, else it will removed inparseInt
– kiranvj
Nov 14 '18 at 3:16
add a comment |
Make sure user input don't have any decimals, else it will removed inparseInt
– kiranvj
Nov 14 '18 at 3:16
Make sure user input don't have any decimals, else it will removed in
parseInt
– kiranvj
Nov 14 '18 at 3:16
Make sure user input don't have any decimals, else it will removed in
parseInt
– kiranvj
Nov 14 '18 at 3:16
add a comment |
There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.
I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though
var total = a*1 + b*1;
var a = "1";
var b = "12.5";
var total = a*1 + b*1;
console.log(total)
add a comment |
There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.
I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though
var total = a*1 + b*1;
var a = "1";
var b = "12.5";
var total = a*1 + b*1;
console.log(total)
add a comment |
There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.
I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though
var total = a*1 + b*1;
var a = "1";
var b = "12.5";
var total = a*1 + b*1;
console.log(total)
There are different methods to do this using parseInt/parseFloat/new Number. See other answers for this.
I use this mostly, multiply the number or string with 1 and then do addition. So far I havent seen any issues. Performance I am not sure though
var total = a*1 + b*1;
var a = "1";
var b = "12.5";
var total = a*1 + b*1;
console.log(total)
var a = "1";
var b = "12.5";
var total = a*1 + b*1;
console.log(total)
var a = "1";
var b = "12.5";
var total = a*1 + b*1;
console.log(total)
answered Nov 14 '18 at 3:13
kiranvjkiranvj
12.7k23451
12.7k23451
add a comment |
add a comment |
2
Change your strings to numbers
parseInt(a) + parseInt(b)
should work as you expect. Read this developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…– Ariel Alvarado
Nov 13 '18 at 22:46