0

Why sum=x+y not working in js

If i wanna addition 1+2 give me 12 insted of 3

13th Nov 2020, 4:25 PM
abdo king
abdo king - avatar
4 Answers
+ 7
Do sum = x + y; And then in another line print sum You should get 3. Since variables in JavaScript are of "flexible" type it may be that you are using the + to concatenate the strings "1" and "2" rather than to add up the numbers 1 and 2.
13th Nov 2020, 4:30 PM
Davide
Davide - avatar
+ 5
wrapped in quotes, it joins/concatenates both strings: "1" + "2" = "12" Without quotes, they act like numbers that have the ability of addition: 1 + 2 = 3 If you wanna convert "12" into a number, either write it without quotes, or use parseInt("12") to get the number 12, or parseFloat if you wanna get the decimal points as well.
13th Nov 2020, 5:04 PM
maf
maf - avatar
+ 4
Your code attempt
13th Nov 2020, 4:41 PM
Matias
Matias - avatar