Javascript Reference - Window btoa() Method








The btoa() method encodes a string in base-64.

Browser Support

btoa Yes Yes Yes Yes Yes

Syntax

window.btoa(str)

Parameter Values

Parameter Description
str Required. The string to be encoded




Return Value

The base-64 encoded string

Example

The following code shows how to Encode a string in base-64.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<!-- w w w  .j  av a  2 s. c  om-->
<script>
function myFunction() {
    var str = "Hello World!";
    var enc = window.btoa(str);

    var res = "Encoded String: " + enc;
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

The code above is rendered as follows: