Type/Since
1.38.0isNil
Checks whether a value is null or undefined and provides a TypeScript type guard for nullish values.
isNil(value):value is null | undefinedImplementation
The function explicitly compares the value with null and undefined using strict equality.
Type guard
The return type `value is null | undefined` makes the function a TypeScript type guard for nullish values.
Nullish values
Only null and undefined return true. Other falsy values such as false, 0, and an empty string return false.
Parameters
valueunknownrequiredThe value to check.
Returns
value is null | undefinedReturns true when the value is null or undefined; otherwise returns false.
Examples
01
Basic usage
Check for nullish values.
TypeScript
isNil(null);
// true
isNil(undefined);
// true
isNil("hello");
// false02
Type narrowing
The type guard narrows the checked value to null | undefined when the condition succeeds.
TypeScript
const value: string | null | undefined = null;
if (isNil(value)) {
// value is narrowed to null | undefined
}03
Falsy values
Other falsy values such as false, 0, and an empty string are not considered nil.
TypeScript
isNil(false);
// false
isNil(0);
// false
isNil("");
// false04
Objects and arrays
Objects and arrays are not considered nil values.
TypeScript
isNil({});
// false
isNil([]);
// falseBuilt with@cyref/js·Official documentation