noNull (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.4 - Diagnostic Category:
lint/nursery/noNull - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/no-null
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noNull": "error" } } }}Description
Section titled “Description”Disallow the use of the null literal.
JavaScript has two values that mean “no value”: null and undefined.
Code that uses both has to check for both, and it’s easy to forget one of them.
Using only undefined keeps missing values consistent across a codebase.
Strict comparisons such as foo === null are allowed, because code often needs to check for null values that come from other libraries or from APIs such as JSON.parse().
Some common APIs require null as an argument and are allowed:
Object.create(null), which creates an object with no prototype.useRef(null)andReact.useRef(null), the usual way to create a React ref for an element.parent.insertBefore(node, null), which addsnodeat the end ofparent.
The fix replaces null with undefined.
It isn’t applied automatically, because other code may check for null with ===, and TypeScript types that include null may not accept undefined.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”let foo = null;code-block.js:1:11 lint/nursery/noNull FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of null.
> 1 │ let foo = null;
│ ^^^^
2 │
ℹ Using both null and undefined for missing values means code has to check for both.
ℹ Use undefined instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with undefined.
1 │ - let·foo·=·null;
1 │ + let·foo·=·undefined;
2 2 │
if (bar == null) {}code-block.js:1:12 lint/nursery/noNull FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of null.
> 1 │ if (bar == null) {}
│ ^^^^
2 │
ℹ Using both null and undefined for missing values means code has to check for both.
ℹ Use undefined instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with undefined.
1 │ - if·(bar·==·null)·{}
1 │ + if·(bar·==·undefined)·{}
2 2 │
function getUser() { return null;}code-block.js:2:12 lint/nursery/noNull FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of null.
1 │ function getUser() {
> 2 │ return null;
│ ^^^^
3 │ }
4 │
ℹ Using both null and undefined for missing values means code has to check for both.
ℹ Use undefined instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with undefined.
1 1 │ function getUser() {
2 │ - ····return·null;
2 │ + ····return·undefined;
3 3 │ }
4 4 │
foo(null);code-block.js:1:5 lint/nursery/noNull FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of null.
> 1 │ foo(null);
│ ^^^^
2 │
ℹ Using both null and undefined for missing values means code has to check for both.
ℹ Use undefined instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace with undefined.
1 │ - foo(null);
1 │ + foo(undefined);
2 2 │
let foo;if (bar == undefined) {}if (bar === null) {}const dictionary = Object.create(null);const ref = useRef(null);parent.insertBefore(node, null);See Also
Section titled “See Also”- To remove
undefinedthat is assigned to a variable when it is declared, seenoUselessUndefinedInitialization.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.