isPrimitive()
isPrimitive(
value): value is string | number | bigint | boolean | symbol | null | undefined
Checks if a value is a primitive (string, number, boolean, null, undefined, symbol, bigint).
Parametersโ
value: unknownโ
The value to check.
Returns: value is string | number | bigint | boolean | symbol | null | undefinedโ
true if the value is a primitive, false otherwise.
Sinceโ
2.0.0
Exampleโ
isPrimitive(1); // => true
isPrimitive('a'); // => true
isPrimitive(true); // => true
isPrimitive(null); // => true
isPrimitive(undefined); // => true
isPrimitive({}); // => false
isPrimitive([]); // => false
isPrimitive(new Date()); // => false
How it works?โ
Type guard that checks if a value is a primitive type.
Primitive Typesโ
Common Checksโ
| Value | Result |
|---|---|
'hello' | true |
123 | true |
true | true |
null | true |
undefined | true |
Symbol() | true |
123n | true |
{} | false |
[] | false |
() => {} | false |
Use Casesโ
Check for simple values ๐โ
Verify if a value is a string, number, boolean, null, undefined, symbol, or bigint. Essential for deep cloning recursion or serialization logic.
if (isPrimitive(value)) {
return value; // Return as-is
}
true
false