+ 4

I don't understand this JavaScript code ! Please explain to me ! Anybody..

how can the result be 75 ? var a = 1; var b = 2; var c = "8"; var d = c + (a=b+1) - c; alert(d);

15th Jun 2017, 6:44 PM
Mugfirfauzy Siregar
Mugfirfauzy Siregar - avatar
5 Antworten
+ 7
Just remember this rule "8"+1=81 "8"-1=7 "5"+2= 52 "5"-2= 3 yeah I also was confused with this.. But JavaScript works like this only... Well u can assume like this + operator treats "8" as string -operator treats "8" as int JavaScript is weird & strange @Siregar
15th Jun 2017, 8:16 PM
Sri Lakshmi
Sri Lakshmi - avatar
+ 7
Here var c="8" we have double quotes here & so 8 is treated as a string Take a look at this : int 8+int 1=9 string 8+int 1= 81 solution is "8"+(a=b+1)="8"+3=83 83-"8"=75 I hope this helps @Siregar
15th Jun 2017, 7:10 PM
Sri Lakshmi
Sri Lakshmi - avatar
+ 4
var d = "8" + (a = 2+1) - "8" => "8" + 3 - "8" => (first we do the +) 83 (string + integer) - "8" => 75 ok?
15th Jun 2017, 7:10 PM
Elias Papachristos
Elias Papachristos - avatar
+ 3
@Elias Papachristos & @Sri Lakshmi so basically, "8" + 3 is two separate number, 8 and 3, not eighty three.. I get that.. but how can (8 and 3) - "8" = 75 ? so (8 and 3) - "8" is same as 83-8 ? It does not make sense to me.. amazing
15th Jun 2017, 7:32 PM
Mugfirfauzy Siregar
Mugfirfauzy Siregar - avatar
+ 2
really weird.. maybe I will found another weirdness in js..
16th Jun 2017, 3:09 AM
Mugfirfauzy Siregar
Mugfirfauzy Siregar - avatar