DEV Community

Discussion on: How do you deal with null vs undefined?

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

I consider the difference to be one of implicitly nonexistent (undefined) vs. explicitly having no value (null). A newly declared variable that has no initial value, vs. an array element that you've cleared, is a suitably illustrative example of the difference.

As for handling them, I consider casting without checks a strict no-no in strongly typed languages like TypeScript. Don't blindly cast... but DO check to make sure the operation you're attempting is valid if you can, and trap the error if you can't.

Collapse
 
johannesjo profile image
Johannes Millan

I consider casting without checks a strict no-no in strongly typed languages like TypeScript

That's an excellent point! Thank you very much! How do you usually deal with (unexpectedly) failing type checks in these scenarios? Do you just throw an error?

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Yep. I error (or skip if optional).