CyrefJS
CCyrefJS
فا
Back to documentation
Type/Since 1.38.0

isNil

Checks whether a value is null or undefined and provides a TypeScript type guard for nullish values.

isNil(value):value is null | undefined

Implementation

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

valueunknownrequired

The value to check.

Returns

value is null | undefined

Returns 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");
// false
02

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("");
// false
04

Objects and arrays

Objects and arrays are not considered nil values.

TypeScript
isNil({});
// false

isNil([]);
// false
Built with@cyref/js·Official documentation