DEV Community

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

Collapse
 
dealloc profile image
Wannes Gennar • Edited

A simple rule that I introduced in the previous company where I worked:
Always initialize variables with null, any encountered undefined was treated as an error.
Typically, we'd only have undefined values when working with externally provided data, and we'd have validation logic on incoming data anyway, which took care of transforming it into null (or throwing an error, depending on which was appropriate).

Simply put, a null is intentionally empty, an undefined indicated that something wasn't 100% right.