3 ms·
As much as some will proclaim neither is required, I think `null` has genuine use when representing data. It's the data-model equivalent of "no preference"; "us
by stephenr 3y ago
As much as some will proclaim neither is required, I think `null` has genuine use when representing data. It's the data-model equivalent of "no preference"; "use the default"; "automatic"; or even "not applicable". That doesn't mean every field in every object/schema should allow null, but there's definitely a use-case for it.
`undefined` to me sounds like a language/engine internal state that ideally shouldn't be relied upon. Whether that means you treat it the same as `null`, I'm unsure of.
JS obviously has `undefined`, but I think it's rare to actually rely on it specifically as different than null; the biggest specific reliance that comes to mind is polyfilling e.g. `if (typeof window.foo === 'undefined'){...}`...; This pattern could just be inverted `typeof window.foo !== 'function'` but could also be just a simple boolean check, `if (!window.foo){...}`.
PHP has something similar with typed properties that have no initial value set - they start with a state of `uninitialized` (whereas untyped properties without an initial value start with a value of `null`). In this case it really is an internal representation that scripts can't really use - trying to fetch the value of an uninitialized property is an error in PHP.