DEV Community

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...
Collapse
 
kimsean profile image
thedevkim

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

value ? 'has value' : 'undefined or null'
Collapse
 
aumayeung profile image
John Au-Yeung

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.

Collapse
 
kimsean profile image
thedevkim • Edited

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

Thread Thread
 
fref profile image
Fredrik Fall

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.

Thread Thread
 
aumayeung profile image
John Au-Yeung

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

Collapse
 
fref profile image
Fredrik Fall

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.

Collapse
 
aumayeung profile image
John Au-Yeung

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.

Collapse
 
diek profile image
diek

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.

Collapse
 
aumayeung profile image
John Au-Yeung

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

Collapse
 
diek profile image
diek

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

Collapse
 
zorianiysapfir profile image
Info Comment hidden by post author - thread only accessible via permalink
Yaroslav Pidmohylniy

OMG, pretty useless article. Just read "You do not know JS" and that is all.

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