ballhogjoni Posted November 14, 2008 Share Posted November 14, 2008 num = 2 and TOTAL_ADULTS = 1 alert(num + TOTAL_ADULTS); When I alert this it gives me 21. It should give me 3. Link to comment https://forums.phpfreaks.com/topic/132644-solved-why-are-these-not-adding/ Share on other sites More sharing options...
rhodesa Posted November 14, 2008 Share Posted November 14, 2008 In javascript, both addition and concatenation use the + symbol. So, if one of the variables is a string (even if it's all numbers in that string) it will use concatenation. Either fine which one is a string, and fix the code earlier to make it an integer...or just wrap the values in parseInt(): <script type="text/javascript"> var num = 2; var TOTAL_ADULTS = '1'; alert(parseInt(num) + parseInt(TOTAL_ADULTS)); </script> Link to comment https://forums.phpfreaks.com/topic/132644-solved-why-are-these-not-adding/#findComment-689850 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.