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. Quote Link to comment 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.