0

Why this result?

Var a=1; Var v=2; Var d="8"; Var c=d+(a=b+1)-d; Alert(c); // 75

18th Nov 2022, 2:20 PM
Luis
6 Answers
+ 3
Ipang I think it's a typo there should be 'v'
18th Nov 2022, 2:43 PM
Random
Random - avatar
+ 2
It's evaluate in this way: d+(v+1)-d ="8"+3-"8" =83-8 =75
18th Nov 2022, 2:42 PM
Random
Random - avatar
+ 2
Where did <b> come from? I got an error message asking that variable when I tested the snippet. How did you get 75?
18th Nov 2022, 2:42 PM
Ipang
+ 2
Oh got it Snehil Pandey
18th Nov 2022, 2:45 PM
Ipang
+ 2
'a' and 'b' are numbers and 'd' is a string. When 'd' adds the reevaluation of 'a', 'a' is silently recast as a string and is concatenated to 'd'. Then, when 'd' is subtracted from 'd', it's silently casted to the number type and completes the operation. If 'd' had the value "foo" for example, the subtraction operation would result in NaN (not a number).
18th Nov 2022, 3:58 PM
Sam
+ 1
Ok. Thanks. Now i got it.
18th Nov 2022, 4:41 PM
Luis