Function isPrimitive

  • Identifies whether the provided value is a JavaScript primitive which is when is it not an object and has no methods or properties. There are 7 primitive data types:

    • string
    • number
    • bigint
    • boolean
    • undefined
    • null
    • symbol

    Most of the time, a primitive value is represented directly at the lowest level of the language implementation.

    All primitives are immutable; that is, they cannot be altered. It is important not to confuse a primitive itself with a variable assigned a primitive value. The variable may be reassigned to a new value, but the existing value can not be changed in the ways that objects, arrays, and functions can be altered. The language does not offer utilities to mutate primitive values.

    Parameters

    • value: any

      The value to check whether it's a primitive value

    Returns value is string | number | bigint | boolean | symbol

    Since

    0.4.4

    Example

    isPrimitive(null);                   // true
    isPrimitive(undefined); // true
    isPrimitive("null"); // true
    isPrimitive("undefined"); // true
    isPrimitive("1"); // true
    isPrimitive("aa"); // true
    isPrimitive(1); // true
    isPrimitive(Number(2)); // true
    isPrimitive(""); // true
    isPrimitive(String("")); // true
    isPrimitive(true); // true
    isPrimitive(false); // true
    isPrimitive("true"); // true
    isPrimitive("false"); // true
    isPrimitive(BigInt(42)); // true
    isPrimitive(Symbol.for("Hello")); // true

    isPrimitive(new String("aa")); // false
    isPrimitive(new Date()); // false
    isPrimitive(_dummyFunction); // false
    isPrimitive([]); // false
    isPrimitive(new Array(1)); // false
    isPrimitive(new Boolean(true)); // false
    isPrimitive(new Boolean(false)); // false
    isPrimitive(new Boolean("true")); // false
    isPrimitive(new Boolean("false")); // false