r/angularjs • u/[deleted] • Apr 09 '19
[General] Kyle Simpson: I’ve Forgotten More JavaScript Than Most People Ever Learn
https://medium.com/@amsterdamjs/kyle-simpson-ive-forgotten-more-javascript-than-most-people-ever-learn-3bddc6c13e9329
u/Asmor Apr 09 '19
For example, completely contrary to most common belief, the == operator is actually more powerful and more useful in our programs than ===, and in fact, usage of == should be preferred and === should only be a last option.
I used JS to teach myself to program in middle school (mid-late 90s). I've never stopped using it since. I love JS. It's my favorite programming language. I use it daily in both my job and my hobby projects.
I understand how type coercion works. I understand how JS's types work. I can't think of a single time I've ever found the coercion of == useful. Not once. The only times I've ever used it were before I really knew JS well enough to understand what it was actually doing.
There are two cases with ==. Either you're relying on the coercion, or you're not.
If you're not relying on coercion, then use ===. It will save your ass, just like you should always "use strict"; on everything you write.
If you are relying on coercion, then you're probably being too clever for your own good. That's like cramming a ternary into the middle of a statement. Yeah, you can. Yeah, the language is specifically designed to allow that. But don't do it. You're just making your code harder to understand.
The only legitimate reason to use == is ignorance.
0
Apr 09 '19
[deleted]
10
u/Asmor Apr 09 '19
I'd still file that under too clever for your own good. I'd either check for null and undefined explicitly or do something like
!foo && foo !== 01
u/el_padlina Apr 10 '19
Why would I want to use === when I know exactly what type of parameters I pass in my function?
0
u/Asmor Apr 10 '19
Ah, I see I've found one of those mythical 10x rock star programmers who has perfect execution and memory and never has bugs in their code.
What's it like? You must make a shitload of money.
1
u/el_padlina Apr 10 '19
No, I just make my functions small enough, and like majority of developers, our code is not used outside of our team.
In your mythical arrogance you have entirely missed the point and the barn behind it.
If you don't know the type of value returned by your own code you have issues.
1
u/Asmor Apr 10 '19
It's arrogance to insist that humans make mistakes and aren't perfect?
TIL.
0
u/el_padlina Apr 10 '19
They do, that's why we have code reviews and many eyes watching the code.
You write small functions, so that you can easily read what's going on.
Both == and === have their place and insisting on always using one or the other seems to be quite pointless.
7
2
2
1
0
21
u/dlrose Apr 09 '19
'most people' learn 0 JavaScript, so if you ever knew any, you too can say this.