What are some really good real world examples for this? (3,4 examples).
Wouldn't the optional operator be enough?
functionfoo(bar?:string){if(!bar){// handle if bar is falsy}// do stuff if value not falsy.}
The only thing I can think of is when you really need the parameter to be exactly null. But I never have this use case in the projects I work on, so I think something is fishy :))
Actually it's just personal preference to have null values instead of undefined.
The example I provided is actually not that great, because I tend to use this more with objects and their properties.
The difference between the two, is that in Maybe the value is required, and in the second one is optional. So, you can have Maybe values and those values would be either defined or not, but would still be required. I have found this to be useful in React.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A really well-written article with good examples. Good job!
Would like to add one more thing I like to use and you might find it interesting:
You should explicitly check for bar===null as in your case the empty string will also make !bar true
you are right, thanks :)
What are some really good real world examples for this? (3,4 examples).
Wouldn't the optional operator be enough?
The only thing I can think of is when you really need the parameter to be exactly
null
. But I never have this use case in the projects I work on, so I think something is fishy :))Actually it's just personal preference to have null values instead of undefined.
The example I provided is actually not that great, because I tend to use this more with objects and their properties.
The difference between the two, is that in Maybe the value is required, and in the second one is optional. So, you can have Maybe values and those values would be either defined or not, but would still be required. I have found this to be useful in React.