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
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.
🌐
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
Encodes any given JavaScript string (string) as UTF-8, and returns the UTF-8-encoded version of the string. It throws an error if the input string contains a non-scalar value, i.e. a lone surrogate.
Author   mathiasbynens
🌐
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).
🌐
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 = utf8encoder.encode(text); }); const resetButton = document.querySelector("#reset"); resetButton.addEventListener("click", () => { window.location.reload(); });
🌐
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.
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-javascript-in-browser
UTF-8 in JavaScript in Browser | Encoding Standards for Programming Languages
Always use TextEncoder to convert strings to UTF-8 bytes and TextDecoder to convert UTF-8 bytes back into strings. The fetch API is smart about UTF-8. When you send data like JSON or plain text, fetch automatically encodes the request body as ...
🌐
Honeybadger
honeybadger.io › blog › encode-javascript
The character encoding cheat sheet for JS developers - Honeybadger Developer Blog
September 21, 2023 - In this comprehensive article, we will explore character encoding in JavaScript, including both Node.js and the browser side. We will start by providing an introduction to character encoding and the Unicode character encoding standard, which has become the de facto standard for encoding text data in modern computing.
Find elsewhere
🌐
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}`;
🌐
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.
🌐
Desarrollo Web
desarrolloweb.com › articulos › codificar-decodificar-cadenas-utf8-javascript.html
Codificar y descodificar cadenas a UTF-8 con Javascript
var texto = "Tomaré una decisión con la cigüeña."; var textoISO = utf8_encode(texto); Como resultado de ejecutar esas instrucciones, el contenido de la variable textoISO volverá a ser la cadena inicial "Tomaré una decisión con la cigüeña.". Como vemos, son funciones bastante simples, pero que podrán sacarnos de algún apuro cuando estamos lidiando con juegos de caracteres desde Javascript y queremos pasar las cadedas entre los habituales ISO-8859-1 y UTF-8.
🌐
npm
npmjs.com › package › utf8
utf8 - npm
December 4, 2017 - Encodes any given JavaScript string (string) as UTF-8, and returns the UTF-8-encoded version of the string. It throws an error if the input string contains a non-scalar value, i.e. a lone surrogate.
      » npm install utf8
    
Published   Dec 04, 2017
Version   3.0.0
🌐
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.
🌐
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 …
🌐
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 ...
🌐
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:
🌐
CodePen
codepen.io › piotezaza › pen › BvvMRm
Encode VS Decode UTF8 JavaScript
<div class="mt-3 text-center"> <h1>Difference between UTF8 encode & decode in JavaScript</h1> <p>Before encode : éàçè / After encode : <span class="encode"></span></p> <p>Before decode : éà çè / After decode : <span class="decode"></span></p> </div>
🌐
JSFiddle
jsfiddle.net › onigetoc › QmT59
javascript utf8 encode-decode - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
🌐
Burke
kevin.burke.dev › kevin › node-js-string-encoding
Let’s talk about Javascript string encoding | Kevin Burke
September 1, 2017 - Unfortunately, sometimes encoding refers to the encoding of string, and sometimes it refers to the encoding of the bytes in the Buffer. Buffer.from('7468697320697320612074c3a97374', 'hex') will decode the input as a series of hex characters, and store the bytes corresponding to each 2-digit hex character in the Buffer. But in var a = 'tést'; Buffer.from(a, 'utf8'); the 'utf8' refers to how the bytes will be stored in the resulting Buffer.