isNil
Strict existence check for null or undefined. Distinct from isEmpty, which also treats empty strings, arrays, objects, Maps, and Sets as empty. isNil returns true only for the two nullish values.
Try it
Section titled “Try it”Import
Section titled “Import”import { isNil } from "1o1-utils";import { isNil } from "1o1-utils/is-nil";Signature
Section titled “Signature”function isNil({ value }: IsNilParams): booleanParameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| value | unknown | Yes | The value to check |
Returns
Section titled “Returns”boolean — true if the value is null or undefined, false otherwise.
Examples
Section titled “Examples”isNil({ value: null }); // => trueisNil({ value: undefined }); // => trueisNil({ value: 0 }); // => falseisNil({ value: "" }); // => falseisNil({ value: false }); // => falseisNil({ value: Number.NaN }); // => false// Default a parameter only when truly absent — preserves 0, "", falsefunction greet(name?: string | null) { const safe = isNil({ value: name }) ? "stranger" : name; return `Hello, ${safe}`;}Edge Cases
Section titled “Edge Cases”- Falsy non-nullish values (
0,"",false,NaN,0n) returnfalse. - Empty arrays, empty plain objects, functions, and symbols are not nil.
- Equivalent to the loose-equality idiom
value == null, with strict-equality semantics for clarity. - Use
isEmptywhen you also need to treat empty containers as missing.
Also known as
Section titled “Also known as”null check, undefined check, nullish check, exists check, defined check, missing value
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use isNil to default a value only when it's null or undefined while preserving other falsy values like 0 and "".