I never understand JavaScript, I’m sure you can all relate.
Why is JavaScript the most confusing web programming language?
Because of this:
5 + 5
You can tell me what this is, right? It’s 10
, that’s right.
What about this?
5 + '5'
This is '55'
. Makes sense, right?
'5' + 5
This is also '55'
. This makes more sense than the last one, though, even though it still makes no sense.
5 + +'5'
Now what’s this? This is 10
again.
'5' - - '5'
This is also 10
.
NaN === NaN
This is false
.
{} + [] === 0 [] + [] === '' [] * 1 === 0 false + 1 === 1
These are all true
.
typeof NaN === 'number'
true
! JavaScript, you’ve been exposed.
JavaScript can be so confusing. It’s not just type juggling you have to worry about. Also, speaking of type juggling:
Use Typescript. These are stupidities caused by JavaScript’s weak typing.
The nastiness of JS is actually a “feature” (rumor has it was a bug…) used in some very specific scenarios. I got used to the language by trying my best to avoid it’s dirt way of doing things and I’m happy with that. Working on frontend you can’t avoid it.