Using TextEncoder and TextDecoder

var uint8array = new TextEncoder("utf-8").encode("Plain Text");
var string = new TextDecoder().decode(uint8array);
console.log(uint8array ,string )
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from PPB on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › TextEncoder › encodeInto
TextEncoder: encodeInto() method - Web APIs | MDN
June 28, 2025 - const encoder = new TextEncoder(); function encodeIntoWithSentinel(string, u8array, position) { const stats = encoder.encodeInto( string, position ? u8array.subarray(position | 0) : u8array, ); if (stats.written < u8array.length) u8array[stats.written] = 0; // append null if room return stats; } ... const sourcePara = document.querySelector(".source"); const resultPara = document.querySelector(".result"); const string = sourcePara.textContent; const textEncoder = new TextEncoder(); const utf8 = new Uint8Array(string.length); const encodedResults = textEncoder.encodeInto(string, utf8); resultPara.textContent += `Bytes read: ${encodedResults.read}` + ` | Bytes written: ${encodedResults.written}` + ` | Encoded result: ${utf8}`;
🌐
GitHub
gist.github.com › joni › 3760795
toUTF8Array: Javascript function for encoding a string in UTF8. · GitHub
toUTF8Array: Javascript function for encoding a string in UTF8. - toUTF8Array.js
🌐
Examplejavascript
examplejavascript.com › utf8 › encode
How to use the encode function from utf8
utf8.encode is a function that encodes a string into a byte array using the UTF-8 character encoding scheme, where each character is represented by one or more bytes. The utf8.encode function converts each character of the string to its ...
Top answer
1 of 3
31

Hi!

When it comes to escape and unescape, I live by two rules:

  1. Avoid them when you easily can.
  2. Otherwise, use them.

Avoiding them when you easily can:

As mentioned in the question, both escape and unescape have been deprecated. In general, one should avoid using deprecated functions.

So, if encodeURIComponent or encodeURI does the trick for you, you should use that instead of escape.

Using them when you can't easily avoid them:

Browsers will, as far as possible, strive to achieve backwards compatibility. All major browsers have already implemented escape and unescape; why would they un-implement them?

Browsers would have to redefine escapeand unescape if the new specification requires them to do so. But wait! The people who write specifications are quite smart. They too, are interested in not breaking backwards compatibility!

I realize that the above argument is weak. But trust me, ... when it comes to browsers, deprecated stuff works. This even includes deprecated HTML tags like <xmp> and <center>.

Using escape and unescape:

So naturally, the next question is, when would one use escape or unescape?

Recently, while working on CloudBrave, I had to deal with utf8, latin1 and inter-conversions.

After reading a bunch of blog posts, I realized how simple this was:

var utf8_to_latin1 = function (s) {
    return unescape(encodeURIComponent(s));
};
var latin1_to_utf8 = function (s) {
    return decodeURIComponent(escape(s));
};

These inter-conversions, without using escape and unescape are rather involved. By not avoiding escape and unescape, life becomes simpler.

Hope this helps.

2 of 3
3

It is never okay to use encodeURI() or encodeURIComponent(). Let's try it out:

console.log(encodeURIComponent('@#*'));

Input: @#*. Output: %40%23*. Wait, so, what exactly happened to the * character? Why wasn't that converted? Imagine this: You ask a user what file to delete and their response is *. Server-side, you convert that using encodeURIComponent() and then run rm *. Well, got news for you: using encodeURIComponent() means you just deleted all files.

Use fixedEncodeURI(), when trying to encode a complete URL (i.e., all of example.com?arg=val), as defined and further explained at the MDN encodeURI() Documentation...

function fixedEncodeURI(str) {
   return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}

Or, you may need to use use fixedEncodeURIComponent(), when trying to encode part of a URL (i.e., the arg or the val in example.com?arg=val), as defined and further explained at the MDN encodeURIComponent() Documentation...

function fixedEncodeURIComponent(str) {
 return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
   return '%' + c.charCodeAt(0).toString(16);
 });
}

If you are unable to distinguish them based on the above description, I always like to simplify it with:

  • fixedEncodeURI() : will not encode +@?=:#;,$& to their http-encoded equivalents (as & and + are common URL operators)
  • fixedEncodeURIComponent() will encode +@?=:#;,$& to their http-encoded equivalents.
Top answer
1 of 10
84

The logic of encoding Unicode in UTF-8 is basically:

  • Up to 4 bytes per character can be used. The fewest number of bytes possible is used.
  • Characters up to U+007F are encoded with a single byte.
  • For multibyte sequences, the number of leading 1 bits in the first byte gives the number of bytes for the character. The rest of the bits of the first byte can be used to encode bits of the character.
  • The continuation bytes begin with 10, and the other 6 bits encode bits of the character.

Here's a function I wrote a while back for encoding a JavaScript UTF-16 string in UTF-8:

function toUTF8Array(str) {
    var utf8 = [];
    for (var i=0; i < str.length; i++) {
        var charcode = str.charCodeAt(i);
        if (charcode < 0x80) utf8.push(charcode);
        else if (charcode < 0x800) {
            utf8.push(0xc0 | (charcode >> 6), 
                      0x80 | (charcode & 0x3f));
        }
        else if (charcode < 0xd800 || charcode >= 0xe000) {
            utf8.push(0xe0 | (charcode >> 12), 
                      0x80 | ((charcode>>6) & 0x3f), 
                      0x80 | (charcode & 0x3f));
        }
        // surrogate pair
        else {
            i++;
            // UTF-16 encodes 0x10000-0x10FFFF by
            // subtracting 0x10000 and splitting the
            // 20 bits of 0x0-0xFFFFF into two halves
            charcode = 0x10000 + (((charcode & 0x3ff)<<10)
                      | (str.charCodeAt(i) & 0x3ff));
            utf8.push(0xf0 | (charcode >>18), 
                      0x80 | ((charcode>>12) & 0x3f), 
                      0x80 | ((charcode>>6) & 0x3f), 
                      0x80 | (charcode & 0x3f));
        }
    }
    return utf8;
}
2 of 10
48

JavaScript Strings are stored in UTF-16. To get UTF-8, you'll have to convert the String yourself.

One way is to mix encodeURIComponent(), which will output UTF-8 bytes URL-encoded, with unescape, as mentioned on ecmanaut.

var utf8 = unescape(encodeURIComponent(str));

var arr = [];
for (var i = 0; i < utf8.length; i++) {
    arr.push(utf8.charCodeAt(i));
}
🌐
GitHub
gist.github.com › boushley › 5471599
A JavaScript UTF-8 decoding function for ArrayBuffers. Credit for most of the heavy lifting goes to "bob" http://ciaranj.blogspot.com/2007/11/utf8-characters-encoding-in-javascript.html · GitHub
A JavaScript UTF-8 decoding function for ArrayBuffers. Credit for most of the heavy lifting goes to "bob" http://ciaranj.blogspot.com/2007/11/utf8-characters-encoding-in-javascrip...
🌐
Blogger
programmerspatch.blogspot.com › 2020 › 04 › converting-utf-8-and-utf-16-arrays-to.html
Programmers’ Patch: Converting UTF-8 and UTF-16 arrays to strings in Javascript and vice versa
April 14, 2020 - <!DOCTYPE html> <head><script> /** * A simple class to convert utf8 or utf16 byte arrays to strings etc * Works in Node.js OR in any browser. No dependencies. */ class unicode { /** * Convert a Uint8Array in UTF-8 to a Javascript string * @param uint8_array a Uint8Array in UTF-8 * @return a Javascript string encoded in standard UTF-16 */ static utf8_to_string(uint8_array) { var str = ""; for ( var i=0;i<uint8_array.byteLength;i++ ) { if ( uint8_array[i] < 128 ) str += String.fromCodePoint(uint8_array[i]); else str += '%'+uint8_array[i].toString(16); } return decodeURIComponent(str); } /** * Convert a javascript string to Uint8Array UTF-8.
Find elsewhere
🌐
Dirask
dirask.com › posts › JavaScript-convert-string-to-bytes-array-UTF-8-1XkbEj
JavaScript - convert string to bytes array (UTF-8)
// ONLINE-RUNNER:browser; const encoder = new TextEncoder('UTF-8'); const toBytes = (text) => { return encoder.encode(text); }; // Usage example: const bytes = toBytes('Some text here...'); // converts string to UTF-8 bytes console.log(bytes); ...
🌐
Coolaj86
coolaj86.com › articles › unicode-string-to-a-utf-8-typed-array-buffer-in-javascript
Unicode String to a UTF-8 TypedArray Buffer in JavaScript
June 26, 2015 - 'use strict'; // string to uint array function unicodeStringToTypedArray(s) { var escstr = encodeURIComponent(s); var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); }); var ua = new Uint8Array(binstr.length); Array.prototype.forEach.call(binstr, function (ch, i) { ua[i] = ch.charCodeAt(0); }); return ua; }
🌐
Designcise
designcise.com › web › tutorial › how-to-convert-a-javascript-byte-array-to-a-string
How to Convert a JavaScript Byte Array to a String? - Designcise
April 9, 2023 - In JavaScript, you can convert an array of bytes to string by using the TextDecoder API with UTF-8 encoding, for example, in the following way:
🌐
Blogger
ciaranj.blogspot.com › 2007 › 11 › utf8-characters-encoding-in-javascript.html
Dazed and Confused: Utf8 Characters Encoding in Javascript Byte Arrays
// based on the code at http://www.webtoolkit.info //************************************************************************************ Utf8Utils= function() { function _encode(stringToEncode, insertBOM) { stringToEncode = stringToEncode.replace(/\r\n/g,"\n"); var utftext = []; if( insertBOM == true ) { utftext[0]= 0xef; utftext[1]= 0xbb; utftext[2]= 0xbf; } for (var n = 0; n < stringToEncode.length; n++) { var c = stringToEncode.charCodeAt(n); if (c < 128) { utftext[utftext.length]= c; } else if((c > 127) && (c < 2048)) { utftext[utftext.length]= (c >> 6) | 192; utftext[utftext.length]= (c
🌐
haikel-fazzani
haikel-fazzani.eu.org › snippet › string-uint8array-conversion
Converting String to Uint8Array & Vice-Versa
October 23, 2024 - Leverages JavaScript’s functional programming features. In Node.js, the Buffer class provides a simple way to handle binary data. const str = 'Hello, World!'; const uint8Array = Buffer.from(str, 'utf-8'); console.log(uint8Array); // Output: <Buffer 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21> ... Optimized for Node.js environments. Supports multiple encodings. ... For advanced use cases, you can use ArrayBuffer and DataView to manually manipulate binary data.
🌐
xjavascript
xjavascript.com › blog › how-to-convert-utf8-string-to-byte-array
How to Convert a UTF8 String to a Byte Array in JavaScript: Handling Multi-Byte Characters with charCodeAt — xjavascript.com
By the end, you’ll understand ... Methods vs. Custom Implementation ... UTF-8 is a variable-length character encoding that represents Unicode code points using 1 to 4 bytes:...
🌐
Burke
kevin.burke.dev › kevin › node-js-string-encoding
Let’s talk about Javascript string encoding | Kevin Burke
September 1, 2017 - Frequently, you want to convert ... the conversion logic in C++ code, which eventually calls out to the V8 binary to handle it. Node offers a Buffer type, where a Buffer is an array ......
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-javascript-in-browser
UTF-8 in JavaScript in Browser | Encoding Standards for Programming Languages
JavaScript strings are internally represented using UTF-16. When you need to work with UTF-8 encoded data, such as sending data over a network or saving it to a file, you'll use the TextEncoder and TextDecoder APIs.
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › utf-8-encoding--javascript-in-browser
UTF-8 Encoding : JavaScript in Browser | Encoding Solutions Across Programming Languages
Learn about UTF-8 encoding in JavaScript, its importance in browsers, and how to implement it effectively for seamless text handling.