docs/reference/predicate/isNull.md
Checks if a value is null.
const result = isNull(value);
isNull(value)Use isNull when you want to check if a value is exactly null. It uses strict equality (===) to recognize only null and not undefined.
import { isNull } from 'es-toolkit/predicate';
// null value
console.log(isNull(null)); // true
// Non-null values
console.log(isNull(undefined)); // false
console.log(isNull(0)); // false
console.log(isNull('')); // false
console.log(isNull(false)); // false
console.log(isNull([])); // false
console.log(isNull({})); // false
It can also be used as a type guard in TypeScript.
function processValue(value: string | null | undefined) {
if (isNull(value)) {
// value is now narrowed to null
console.log('Value is null');
} else {
// value is narrowed to string | undefined
console.log('Value is not null:', value);
}
}
isNull is different from isNil in that it treats undefined as false.
import { isNil, isNull } from 'es-toolkit/predicate';
console.log(isNull(undefined)); // false
console.log(isNil(undefined)); // true
value (unknown): The value to check if it's null.(value is null): Returns true if the value is null, false otherwise.