Adding number and string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Adding number and string

Sometimes numbers are adding like numbers, sometimes like strings. Is there a rule about it? https://code.sololearn.com/W9cK75LnEyOS/?ref=app

14th May 2019, 2:28 PM
Sławek J.
5 Answers
+ 8
But in first example are 2numbers 0 & 7; and they are adding like strings
14th May 2019, 4:46 PM
Sławek J.
+ 5
1+1+"j" //type string output:2j because the number before string "j"+1+1//type string output:11j because the string before number
14th May 2019, 4:43 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 5
So why 1+1+ "j" is not 1+"1j", = 11j; it makes no sense
14th May 2019, 7:32 PM
Sławek J.
+ 4
Sławek J. The type becomes a string so in the example 7+""+7 is string and 0 + string = 077
14th May 2019, 6:51 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 4
See brother: a=0; a+=1+""; // first it starts in 1+"" And 1+"" = 1 but in string like 1+1*2=3 because multiplication before the addition so 0 + 1 in string == 01 and in yours ==077
15th May 2019, 2:42 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar