Function uniqueId

  • Generates a unique identifier, optionally prefixed with a given string.

    Parameters

    • Optionalprefix: string

      An optional string to prefix the unique identifier. If not provided or not a string, only the unique numeric identifier is returned.

    Returns string

    A string containing the unique identifier, with the optional prefix if provided.

    // Generate a unique ID with a prefix
    uniqueId('user_'); // => 'user_1'
    // Generate a unique ID without a prefix
    uniqueId(); // => '2'
    // Subsequent calls increment the internal counter
    uniqueId('item_'); // => 'item_3'
    uniqueId(); // => '4'