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.
So JavaScript type coercion is odd, but if you understand it and the WHY, it isn’t that bad.
Complain about this instead: https://jsdate.wtf/
God, this is cursed
To make it even worse
Mh, '0' is a nonempty string, so !'0' returns false. Then of course !(!'0') would return true. I'd absolutely expect this, Python does the same.
And the second thing is just JavaScript's type coercion shenanigans. In Python
bool('0') # returns True because of nonempty string bool(int('0')) # returns False because 0 == False
Knowing that JavaScript does a lot of implicit type conversions, stuff like that doesn't strike me as very surprising.
wait hang on...
Javascript's type coercion is rather insane, yes, but there is an actual, practical reason it's done. JS, having been designed to be run in web browsers, wants to avoid blowing up and crashing at all costs. If it gets an unusual type comparison, usually the result of a bug, it tries to return something, such that the script can continue running if at all possible. In JS' mentality, keeping a page running, even if it might not completely function properly, is preferable to throwing an unhandled exception and completely crashing it.
Whether or not that is the right approach is debatable, but there is at least some logic to it. Personally, I think that the proliferation of Node letting JS run outside of browsers exacerbates a lot of JS' issues, but TypeScript does a lot to make it look like a more sensible language.
This is Haverbeke's book, right? Eloquent JavaScript?
It's from Javascript: The Definitive Guide 7th Edition by David Flanagan. It's the O'Reilly book with the rhinoceros on it.
It's the O'Reilly book with the rhinoceros on it.
Honestly O'Reilly should just remove the titles and leave just the animals to describe the books.
This makes it make so much more sense...
Its not really insanity, just a lot of hidden function calls
Hey OP, do you mind checking if your book explains the type coercions that are used with the +
operator? I remember it also being mind-boggling, so I was hoping you book could demystify it too.
I don't recall if it covers that sadly. I read it months ago and this part stood out to me.
The description in the first photo about int - steing comparison is incomplete though, right? Wasn't there also a rule anout which one of then comes first (the second parameter gets converted?), and what happens if a string contains non-numeric values?
It's all so confusing…
I mean, it is the basic concept of truthiness. At least they let you use strict equal.
But then why 2 == true if true is converted to 1
3 - 1 // -> 2
3 + 1 // -> 4
'3' - 1 // -> 2
'3' + 1 // -> '31'
Is that a tear on the "again"?
Or sweat. Either is appropriate there.
why convert boolean to integer instead of converting the other operand to boolean? it doesn't make sense.
Hello from Perl! Looks reasonable to me!
Yes, we should all use rigid types. Name me one language you actually like writing quickly with that has types?
Pyth-oh. Bash-oh. Lisp-oh. Perl-oh. Oh yeah... typed languages suck because of all the boiler
Edit: Fine, Python / Lisp / Perl are all technically "typed" languages, but I ask you what's point of throwing type errors at runtime. Javascript and Rust actually have it right here that the code is either going to run, or it simply isn't. No pussyfooting letting it run first to throw complaints
Fortran.
No I'm not joking.
Zig
Python, uh, has types tho? Python just doesn't engage in the same brainless type coercion that JS does, instead type coercion is much more limited and sensible.
Haskell.
Never got into Haskell, but I was taught Miranda at school and thought it was pretty cool
Python. Don't know why you excluded it.
I didn't, I'm using the current nomenclature: https://en.wikipedia.org/wiki/Python_(programming_language)#History (last paragraph)
Python is un(i)typed.
Kotlin is pretty good
So this is what insanity looks like.