-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
null vs undefined #71
Comments
Sindre Sorhus is thinking about the same: sindresorhus/meta#7 |
Just for the record:
|
jhnns
added a commit
that referenced
this issue
Feb 5, 2020
Forbids the usage of `null`. In a codebase it's often better to use a single non-value to represent *the absence of a value*. With the rise of default parameters and destructuring defaults, JavaScript developed a clear tendency towards `undefined`. [This issue](#71) summarizes the arguments (and trade-offs) of **null vs. undefined**. **Please note:** If you use this rule, you will probably still need a single `null` value which you can refer to whenever you need to use `null` because of third-party code: ```js // eslint-disable-next-line no-null/no-null export const NULL = null; ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to start a discussion about
null
andundefined
. In the end I would like to use only one value to represent a non-value.Motivation
In my experience it's problematic when developers start to use both to represent the absence of a value. When a value can either be
null
orundefined
or something else, people start to write code like this:This is problematic because it may introduce bugs where
someValue
is casted implicitly tofalse
(e.g. with0
or""
). The alternative would be to use:which conflicts with our rule to use strict equality checks. It's also not intuitive that
== null
will also check forundefined
. The biggest problem to me is that== null
is also not optimizable for the browser becausedocument.all == null
might also evaluate totrue
(this is a little known fact).The last alternative would be to write:
which is just annoying.
But there are more problems. JS default parameters for functions and destructuring will only work with
undefined
. This makes it even more complicated if the codebase is mixingundefined
andnull
. On the other hand,JSON.stringify
will just omitundefined
values.A codebase cannot ignore these issues in my experience. It should stick to a guideline.
Comparison
Pro
undefined
undefined
undefined
typeof "object"
works as intended2 + null; // 2
2 + undefined; // NaN
Pro
null
null
null
for rendering nothing (undefined
is not supported)null
return
andreturn null
(although it's not a good style to mix both in the same function)obj.prop === undefined
means it doesn't existobj.prop === null
means it exists but it's not setMy conclusion
For a long time (especially pre-ES2015) I was pro
null
because setting something tonull
explicitly was a clear sign that "there is something, it's just not set". It maps clearly to the concept of known knowns, known unknowns (null
) and unknown unknowns (undefined
).However, with the rise of ES2015 and the widespread use of default parameters and values, the usability problems became more apparent. In one of our projects we had to do something like this just to use default props:
Since this is too much code to write and read, people will either deviate from the rule to use
null
and useundefined
where it's easier for them (eventually leading to the situation I mentioned at the beginning) or use unsafe boolean expressions like:Nullish Coalescing will make that less error prone, but it's still unnecessary code in my opinion.
So my proposal would be to ban
null
and only useundefined
. Theeslint-plugin-no-null
rule could enforce that.For situations where developers need to use
null
(e.g. when returning nothing from React components) we can still have a single module that exports anull
value:For situations where developers need to save
undefined
to a JSON it's possible to have a utility function that converts all explicitundefined
properties intonull
before sending it toJSON.stringify()
. For consistency reasons you would also need the opposite forJSON.parse()
. The null module and these helper functions could be published to NPM so that you don't have to write or copy that again.I would like to add the
no-null/no-null
rule as a style rule to our ESLint config. This means that we can try it out in a project and make some experience with it.Side notes
Defining optional properties with
undefined
is easier in TypeScript:vs
What do you think?
Other opinions:
The text was updated successfully, but these errors were encountered: