DEV Community

Discussion on: You don't need null

Collapse
 
loucyx profile image
Lou Cyx
  1. Yes it does, when you skip a value, tada! That's undefined 😉
  2. Because JS folks decided to do so. Other languages that only have a single nullish value use that for any scenario that requires nullish values. From JS we can also just use one, and what I say in this article is that you can just use undefined.
Collapse
 
martinpham profile image
Martin Pham
  1. As I replied to other guy. The response should be consistent, if you skip this value, tada! an error should be thrown. Because you won’t know if the value is missing (eg: it has, but api fails to give it) or the value is not defined.
  2. Other languages which don’t have it because they are more strict, where it’s bad when you try to use an undefined variable. JS was born as a mess, but growing up as a flexible language, that’s why we have undefined and null to use together
 
loucyx profile image
Lou Cyx
  1. If the API fails, you should handle that in other ways (response headers and proper error handling), if the API responded with the wrong body, that will be a problem either if you use null or undefined, so I still don't see much value for null in this scenario. If a value can either come or not, then you can define it as optional (meaning Type | undefined), and in the front end you can just do value ?? "default value" or if (value === undefined) { throw // ... } or whatever 😄
  2. You should take a look at optional chaining and nullish coalescing, saying "it's bad when you try to use an undefined variable" feels like you might not be aware of this operators, but long story short, it's no longer bad. You can even "call" undefined 🎉
 
martinpham profile image
Martin Pham
  1. Of course you can handle it anyway, but how you know if the value is missing (an error), or if the value is not set?
  2. I was saying that about other languages, not js

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