DEV Community

Discussion on: You don't need null

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