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.

Answer from Sumukh Barve on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › TextEncoder
TextEncoder - Web APIs - MDN Web Docs
June 28, 2025 - This example shows how to encode the "€" character to UTF-8. ... const utf8encoder = new TextEncoder(); const text = "€"; const output = document.querySelector("#output"); const encodeButton = document.querySelector("#encode"); encodeButton.addEventListener("click", () => { output.textContent ...
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.
🌐
Examplejavascript
examplejavascript.com › utf8 › encode
How to use the encode function from utf8
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 if (data.message_text && data.telegram_id) { var apiTelegram = "https://api.telegram.org/bot"; apiTelegram += PublikFungsi.botTelegram + "/sendMessage?"; apiTelegram += "parse_mode=HTML"; apiTelegram += "&chat_id=" + data.telegram_id; apiTelegram += "&text=" + utf8.encode(data.message_text); fetch(apiTelegram) .then((response) => response.json()) .then((data_json) => {
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURI
encodeURI() - JavaScript - MDN Web Docs
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).
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-javascript-in-browser
UTF-8 in JavaScript in Browser | Encoding Standards for Programming Languages
Here's a practical example: const encoder = new TextEncoder(); const decoder = new TextDecoder(); const originalString = "JavaScript 😊"; const utf8Bytes = encoder.encode(originalString); console.log(utf8Bytes); // A Uint8Array containing ...
🌐
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}`;
🌐
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.
🌐
JSFiddle
jsfiddle.net › onigetoc › QmT59
javascript utf8 encode-decode - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
Find elsewhere
🌐
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. Here’s a simple example:
🌐
Webtoolkit
webtoolkit.info › javascript_utf8.html
Javascript UTF-8 - Javascript tutorial with example source code
November 17, 2013 - The encoding known today as UTF-8 was invented by Ken Thompson. UTF-8 is a variable-length character encoding for Unicode. It can represent any character in the Unicode standard, yet is backwards compatible with ASCII. Use this Javascript to encode decode UTF-8 data.
🌐
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
// U+00A9 COPYRIGHT SIGN; see http://codepoints.net/U+00A9 utf8.encode('\xA9'); // → '\xC2\xA9' // U+10001 LINEAR B SYLLABLE B038 E; see http://codepoints.net/U+10001 utf8.encode('\uD800\uDC01'); // → '\xF0\x90\x80\x81'
Author   mathiasbynens
🌐
Honeybadger
honeybadger.io › blog › encode-javascript
The character encoding cheat sheet for JS developers - Honeybadger Developer Blog
September 21, 2023 - However, in JavaScript, strings are encoded using the UTF-16 standard, which can cause issues when working with other character encodings. When reading and writing text files in Node.js, you can specify the type of character encoding using the fs module. For example, to read a file as UTF-8, you can use the following code:
🌐
GitHub
gist.github.com › tudisco › 309479 › 0943271466e3c511b7ff88567469eb5ba7d50b39
javascript utf8 encode and decode · GitHub
javascript utf8 encode and decode · Raw · Javascript UTF8 Encode Decode Webkit · 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.
🌐
GitHub
gist.github.com › ghtweb › 7896599
utf8_encode.js · GitHub
December 10, 2013 - utf8_encode.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. Learn more about bidirectional Unicode characters ·
🌐
Blogger
ecmanaut.blogspot.com › 2006 › 07 › encoding-decoding-utf8-in-javascript.html
ecmanaut: Encoding / decoding UTF8 in javascript
function encode_utf8(s) { return unescape(encodeURIComponent(s)); } function decode_utf8(s) { return decodeURIComponent(escape(s)); } 2012 Update: Monsur Hossain took a moment to explain how and why this works. It's a good, in-depth post citing all standards in play so you need not bring a wizard's beard to know why it works. Executive summary: escape and unescape operate solely on octets, encoding/decoding %XXs only, while encodeURIComponent and decodeURIComponent encode/decode to/from UTF-8, and in addition encode/decode %XXs.
🌐
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:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURIComponent
encodeURIComponent() - JavaScript - MDN Web Docs
The encodeURIComponent() 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).
🌐
W3Resource
w3resource.com › javascript › functions › encodeURI-function.php
JavaScript encodeURI function - w3resource
August 19, 2022 - The following web document encodes a specified string using the encodeURI() function. ... str1 = "https://www.w3resource.com/javascript tutorial.html"; console.log(str1); str2 = encodeURI("https://www.w3resource.com/javascript tutorial.html"); ...