Skip to content

Function: isPrimitive()

ts
function isPrimitive(value): boolean;

Checks if a value is a JavaScript primitive type. 检查值是否为JavaScript原始类型。

Parameters

ParameterTypeDescription
valueunknownThe value to check / 要检查的值

Returns

boolean

True if the value is a primitive type / 如果值是原始类型则返回true

Example

typescript
// Primitive types
isPrimitive('') // => true
isPrimitive('hello') // => true
isPrimitive(0) // => true
isPrimitive(42) // => true
isPrimitive(true) // => true
isPrimitive(false) // => true
isPrimitive(null) // => true
isPrimitive(undefined) // => true
isPrimitive(Symbol()) // => true
isPrimitive(BigInt(123)) // => true

// Non-primitive types (objects)
isPrimitive({}) // => false
isPrimitive([]) // => false
isPrimitive(function() {}) // => false
isPrimitive(new Date()) // => false
isPrimitive(new Error()) // => false
isPrimitive(/regex/) // => false

Since

1.0.0

Released under the MIT License.