r/webdev • • Jun 17 '18

My most hated JavaScript features

https://medium.com/@tabu_craig/my-worst-javascript-features-465378d5ad43
0 Upvotes

3 comments sorted by

2

u/birjolaxew Jun 17 '18

I think your problem with null mostly stems from you misunderstanding what null means in JS. You see null as being equal to undefined. It isn't. undefined should be seen as an indication that the value is unset, while null should be seen as an indication that the value was explicitly set to be nothing (as described by MDN: "The value null represents the intentional absence of any object value").

It is important to maintain this explicit non-value when getting e.g. default values - if you have e.g. a field in your storage indicating the gender of a user, and you give the user the option of choosing "would rather not disclose", then you would use null to represent this non-value. Defaulting to one of the genders would go against the point of using null. When you initialize the field without knowing what the user's gender is, you set it to undefined.

1

u/empire539 Jun 17 '18

Exactly this. I recently had a real-life use case where I needed to assign people to certain locations. For the locations I explicitly left empty, I assigned null. For the locations I hadn't yet assigned, they were undefined.

1

u/govision Jun 17 '18

It's non-reusable code and when big enough it's not really readable. Hence ... typescript