Sleepless One@lemmy.ml to Programmer Humor@lemmy.mlEnglish · 1 day agoThe JavaScript type coercion algorithmlemmy.mlimagemessage-square42fedilinkarrow-up1106arrow-down12file-textcross-posted to: programmer_humor@programming.dev
arrow-up1104arrow-down1imageThe JavaScript type coercion algorithmlemmy.mlSleepless One@lemmy.ml to Programmer Humor@lemmy.mlEnglish · 1 day agomessage-square42fedilinkfile-textcross-posted to: programmer_humor@programming.dev
minus-squareretrolasered@feddit.uklinkfedilinkEnglisharrow-up3arrow-down1·1 day agoBut then why 2 == true if true is converted to 1
minus-squareMidnitte@beehaw.orglinkfedilinkEnglisharrow-up7arrow-down1·edit-21 day ago3 - 1 // -> 2 3 + 1 // -> 4 '3' - 1 // -> 2 '3' + 1 // -> '31'
minus-squareTelemachus93@slrpnk.netlinkfedilinkarrow-up6·1 day agoIt’s not. Just tried in my Browser Console: 2 == true // returns false
minus-squareretrolasered@feddit.uklinkfedilinkEnglisharrow-up5·1 day agoI genuinely wasn’t aware of that. I must be getting javascript confused for almost any other language. I wonder how many times ive !!'d a value to make that work without actually absorbing that into my head now…
minus-squareExcel@beehaw.orglinkfedilinkEnglisharrow-up2·20 hours ago2 is not == true, but !!2 is true
minus-squareTelemachus93@slrpnk.netlinkfedilinkarrow-up2·21 hours agoIn other languages that shouldn’t be equal either though, right? Maybe you meant if (2){ console.log("nonzero ints are truthy") } else { console.log("no they're not") } Which would output nonzero ints are truthy and that would actually work in all languages I know. But that’s different from being equal.
minus-squareretrolasered@feddit.uklinkfedilinkEnglisharrow-up1·8 hours agoYeah its checking for not null with if isnt it. Maybe thats what has me confused
But then why 2 == true if true is converted to 1
3 - 1 // -> 2
3 + 1 // -> 4
'3' - 1 // -> 2
'3' + 1 // -> '31'
It’s not. Just tried in my Browser Console:
2 == true // returns false
I genuinely wasn’t aware of that. I must be getting javascript confused for almost any other language. I wonder how many times ive !!'d a value to make that work without actually absorbing that into my head now…
2 is not == true, but !!2 is true
In other languages that shouldn’t be equal either though, right?
Maybe you meant
if (2){ console.log("nonzero ints are truthy") } else { console.log("no they're not") }
Which would output
nonzero ints are truthy
and that would actually work in all languages I know. But that’s different from being equal.
Yeah its checking for not null with if isnt it. Maybe thats what has me confused