Function toBase64

  • Encodes a string into Base64 format.

    Automatically handles the environment:

    • In Node.js (no window), uses Buffer.
    • In browsers, uses window.btoa.

    Parameters

    • str: string

      The input string to encode.

    Returns string

    The Base64-encoded representation of the input string.

    const encoded = toBase64("Hello World");
    console.log(encoded); // "SGVsbG8gV29ybGQ="