I'd recommend using Buffer:

Buffer.from('someString', '<input-encoding>').toString('utf-8')

This avoids any unnecessary dependencies that other answers require, since Buffer is included with node.js, and is already defined in the global scope.

Answer from Lord Elrond 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 - The TextEncoder.encodeInto() method takes a string to encode and a destination Uint8Array to put resulting UTF-8 encoded text into, and returns an object indicating the progress of the encoding. This is potentially more performant than the encode() method — especially when the target buffer ...
🌐
GitHub
github.com › mathiasbynens › utf8.js
GitHub - mathiasbynens/utf8.js: A robust JavaScript implementation of a UTF-8 encoder/decoder, as defined by the Encoding Standard. · GitHub
Unlike many other JavaScript solutions, it is designed to be a proper UTF-8 encoder/decoder: it can encode/decode any scalar Unicode code point values, as per the Encoding Standard. Here’s an online demo. Feel free to fork if you see possible improvements! ... Encodes any given JavaScript string (string) as UTF-8, and returns the UTF-8-encoded version of the string.
Starred by 566 users
Forked by 114 users
Languages   JavaScript 87.4% | Python 10.1% | HTML 2.5%
🌐
Medium
medium.com › @vincentcorbee › utf-16-to-utf-8-in-javascript-18b4b11b6e1e
UTF-16 to UTF-8 in Javascript. How are strings in Javascript encoded… | by Vincentcorbee | Medium
August 5, 2025 - UTF-16 to UTF-8 in Javascript How are strings in Javascript encoded? It might surprise some people that Javascript uses UTF-16 to encode strings. But if your are like me, you might be wondering: how …
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › utf-8-encoding--javascript-in-browser
UTF-8 Encoding : JavaScript in Browser | Encoding Solutions Across Programming Languages
Encoding text in UTF-8 format in JavaScript can be done using the TextEncoder API, which is supported in most modern browsers. The TextEncoder class allows you to convert strings into a sequence of bytes encoded in UTF-8.
🌐
n8n
community.n8n.io › questions
Convert into utf-8 - Questions - n8n Community
July 3, 2024 - Hello, Coming back to n8n again after short break…!! I wanted to create one function in CODE node… convert string into utf-8 encode. convert string into base64 encode. for this, i used - Buffer.from(str).toStrin…
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURI
encodeURI() - JavaScript - MDN Web Docs
July 8, 2025 - The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two surrogate characters).
Find elsewhere
🌐
Burke
kevin.burke.dev › kevin › node-js-string-encoding
Let’s talk about Javascript string encoding | Kevin Burke
September 1, 2017 - Where the cent character is the UTF-8 encoded byte sequence "\xc2\xa2". When Node starts and you try to reference x in your program, it will be re-encoded as a UTF-16 string. If you type the literal characters: ... This will be turned into the UTF-16 string "\xc2\x00\xa2\x00". So be careful to mind your inputs and outputs. Encoding in Node is extremely confusing, and difficult to get right. It helps, though, when you realize that Javascript string types will always be encoded as UTF-16, and most of the other places strings in RAM interact with sockets, files, or byte arrays, the string gets re-encoded as UTF-8.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Honeybadger
honeybadger.io › blog › encode-javascript
The character encoding cheat sheet for JS developers - Honeybadger Developer Blog
September 21, 2023 - Next, we create a new string called str by decoding the buf buffer using the utf-8 encoding with the iconv.decode function. Finally, we log the str string to the console using the console.log function. When working with character encoding in JavaScript, there are a few best practices to keep in mind:
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.
🌐
Wu Wenjun
tie.pub › en › snippets › js › encode-utf16-to-utf8
Encode a String to UTF-8 - Wu Wenjun
May 18, 2026 - /** * Encode UTF16 to UTF8. * See: https://gist.github.com/joni/3760795 * @param str {string} * @returns {Array} UTF8 array */ function toUTF8Array(str) { const utf8 = []; for (let i = 0; i < str.length; i++) { let 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
🌐
GitHub
gist.github.com › chrisveness › bcb00eb717e6382c5608
Utf8 string encode/decode using regular expressions · GitHub
str="\\320\\223..."; Utf8Decode(str.replace(/\\[0-9][0-9][0-9]/g,function(s){return String.fromCharCode(parseInt(s.substr(1),8));})) If your string can already contain UTF-16 chars you need to first encode those into UTF-8:
🌐
npm
npmjs.com › package › utf8
utf8 - npm
December 4, 2017 - Unlike many other JavaScript solutions, it is designed to be a proper UTF-8 encoder/decoder: it can encode/decode any scalar Unicode code point values, as per the Encoding Standard. Here’s an online demo. Feel free to fork if you see possible improvements! ... Encodes any given JavaScript string (string) as UTF-8, and returns the UTF-8-encoded version of the string.
      » npm install utf8
    
Published   Dec 04, 2017
Version   3.0.0
🌐
GitHub
gist.github.com › afcc49877d0a5d1ac8cbb3373303c719
Converting to UTF-8 with javascript - Gist - GitHub
Converting to UTF-8 with javascript · Raw · utf8conversion.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
DEV Community
dev.to › mistval › beware-of-emoji-in-js-144o
JavaScript String Encoding Gotchas - DEV Community
January 21, 2022 - But for fs to do that, it does a conversion from UTF-16 to UTF-8 before writing to the file. Basically, there can be a difference between the encoding used to store strings in memory in JavaScript and the encoding that libraries like fs choose to use by default for I/O.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › TextEncoder
TextEncoder - Web APIs - MDN Web Docs
June 28, 2025 - The TextEncoder interface enables you to encode a JavaScript string using UTF-8.