The JavaScript type coercion algorithm
The JavaScript type coercion algorithm



That's it. That's the meme.
The JavaScript type coercion algorithm
That's it. That's the meme.
You're viewing a single thread.
But then why 2 == true if true is converted to 1
It's not. Just tried in my Browser Console:
undefined
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..
In other languages that shouldn't be equal either though, right?
Maybe you meant
undefined
if (2){ console.log("nonzero ints are truthy") } else { console.log("no they're not") }
Which would output
undefined
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
2 is not == true, but !!2 is true
3 - 1 // -> 2
3 + 1 // -> 4
'3' - 1 // -> 2
'3' + 1 // -> '31'
2 is 'truthy', or rather, not 'falsy'.
I'm not even kidding.