Skip to content

noNull (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noNull": "error"
}
}
}
}

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) and React.useRef(null), the usual way to create a React ref for an element.
  • parent.insertBefore(node, null), which adds node at the end of parent.

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.

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);