Global / Window

btoa

Creates a Base64-encoded ASCII string from a binary string

Syntax

JavaScript
btoa(stringToEncode)

Parameters

ParameterTypeDescription
stringToEncodestringThe binary string to encode

Return Value

An ASCII string containing the Base64 representation

Examples

Basic Usage
const encoded = btoa('Hello World');
console.log(encoded); // 'SGVsbG8gV29ybGQ='
Practical Example
const data = JSON.stringify({ name: 'Alice', age: 30 });
const base64 = btoa(data);
console.log(base64); // 'eyJuYW1lIjoiQWxpY2UiLCJhZ2UiOjMwfQ=='
Advanced Usage
function createBasicAuth(user: string, pass: string) {
  return 'Basic ' + btoa(`${user}:${pass}`);
}
console.log(createBasicAuth('admin', 'secret'));

Understanding btoa

The btoa method in JavaScript creates a Base64-encoded ASCII string from a binary string. It belongs to the window object and is one of the most widely used methods for working with window values in modern JavaScript and TypeScript applications.

The method signature is btoa(stringToEncode). It accepts 1 parameter: stringToEncode. When called, it returns an ascii string containing the base64 representation. Understanding when and how to use btoa() helps you write more expressive, readable code.

Common use cases for btoa include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like window-atob, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for btoa is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Global / Window Methods

Other methods in the Global / Window object

Related Tools

More Global / Window Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.