+ 1

Type conversion: why is this true?

var a = (1==="1"?false:true); alert(a); //true I thought === meant ‘alike in both content and type’ therefore, how can it be true that 1===“1”? Ah, wait... is it because the statement, even though 1 ===“1” is false, the Ternary statement has the term ‘true’ to be returned since it is in the final position in the statement?

10th Mar 2019, 3:03 PM
Dan Leighton
Dan Leighton - avatar
3 odpowiedzi
+ 1
Well, either at fault or very sneaky quiz question! ;-)
10th Mar 2019, 3:42 PM
Dan Leighton
Dan Leighton - avatar
0
It's a normal result. Here it's used identity operator "===" checks not only value , but also type => number and string (different types) so Boolean check is false => the value of a is true (after ":", because the boolean check is false). You can find more info here: https://www.w3schools.com/js/js_comparisons.asp Hope it helps you.
10th Mar 2019, 3:53 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Indeed. That is the answer I came to after thinking about it :-)
10th Mar 2019, 4:01 PM
Dan Leighton
Dan Leighton - avatar