Skip to main content

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โ€‹

ValueResult
'hello'checkmark true
123checkmark true
truecheckmark true
nullcheckmark true
undefinedcheckmark true
Symbol()checkmark true
123ncheckmark true
{}cross false
[]cross false
() => {}cross 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
}