More JavaScript Mistakes You May be Making

John Au-Yeung on January 14, 2020

Subscribe to my email list now at http://jauyeung.net/subscribe/ Follow me on Twitter at https://twitter.com/AuMayeung Many more articles at http... [Read Full]
markdown guide

its also possible to check the value if its null or undefined with this.

value ? 'has value' : 'undefined or null'

This wouldn't be 100 percent reliable since 0, empty string, and NaN are also falsy.

So you would get the second choice returned in those cases as well.


Yea, i guess you could just use the snippet if you wanted to check if theres really a value or not. I think it could be useful sometimes when you are getting a value from an api and you don't have access into the backend funtions, and they just throw either undefined null 0 or empty string

My view is to have undefined something only the runtime is allowed to set, while null is something I deliberately set.
This way you get a hint who messed up if the value is missing.

As long as we remember the difference, I think it's OK. Also, we have to be consistent in how we use them.


Nice article!

To add to the part about addition/concatenation, the underlying problem
is javascript's automatic type-convertions. The basis for the infamous "WAT" video on youtube (recommended).

String concatenation is also being replaced by template literals for projects employing the newer ES versions.


Yea. Type coercion is tricky. I think we just have to convert everything to the same type so that we won't make mistakes with we apply operators to them.


I saw an interview when the code they were asking to explain, has the broken line return. That was an unexpected behavior at first look.


Breaking return statement is most likely a mistake since the code below is unreachable since the function already stopped running after the return line.


I know, but at first look can be confusing. It is obvious when you know it, like the comparation with coercion types xD

Some comments have been hidden by the post's author - find out more

code of conduct - report abuse