Why does this print "76"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why does this print "76"?

From a JS Challenge, the following code var x = 7 + "2"; var y = 4 - "8"; alert(x-y); alerted "76". Can someone explain why?

9th Sep 2017, 4:51 PM
Ioanna Zapalidi
2 Answers
+ 7
var x =7 +"2" wihich gives 72 var y = 4 - "8" which gives -4 in the first line the 7 and 2 are concatenated but in secon line the "-" sign doesn't concatenate the two values so it converts "8" to int.. and yields -4
9th Sep 2017, 4:58 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
thanks! that's a nice way to show when they concatenate or not!
9th Sep 2017, 5:01 PM
Ioanna Zapalidi