The global escape() and unescape() functions are legacy/deprecated. Keep them only when maintaining old code. For new URL encoding work, use encodeURI(), encodeURIComponent() and their matching decode functions.
const value = "name=Subhendu Mohapatra & city=Hyderabad";
console.log(encodeURIComponent(value));
const text = "A B+C";
console.log(escape(text)); // legacy onlyescape() creates old-style percent/Unicode escape sequences and should not be chosen for new applications.
const encoded = escape("A B+C");
console.log(unescape(encoded)); // legacy pairunescape() is the corresponding legacy decoder.
const url = "https://example.com/search?q=hello world";
console.log(encodeURI(url));encodeURI() preserves characters that have structural meaning in a URI.
const query = "JavaScript strings & arrays";
const url = "https://example.com/search?q=" + encodeURIComponent(query);
console.log(url);
const encoded = encodeURIComponent("A&B");
console.log(decodeURIComponent(encoded)); // A&B
URL encoding does not make arbitrary input safe for HTML, SQL, JavaScript, or another context. Use the escaping/validation appropriate to the destination context.
Previous / String Reference Next related topic
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| manoj | 09-07-2015 |
| I have "&" in my query string(ex: str="avm=1&av=m&noj&ss=1"). With unescape I am doing split(unescape(str).split('&')), then I am not able to get back same key value pair. How can I get my string "m&noj" back in this scenario. | |