JavaScript escape() and unescape(): Legacy Reference

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));

What escape() did Top ↑

const text = "A B+C";
console.log(escape(text)); // legacy only

escape() creates old-style percent/Unicode escape sequences and should not be chosen for new applications.

What unescape() did Top ↑

const encoded = escape("A B+C");
console.log(unescape(encoded)); // legacy pair

unescape() is the corresponding legacy decoder.

encodeURI() for a complete URI Top ↑

const url = "https://example.com/search?q=hello world";
console.log(encodeURI(url));

encodeURI() preserves characters that have structural meaning in a URI.

encodeURIComponent() for one component Top ↑

const query = "JavaScript strings & arrays";
const url = "https://example.com/search?q=" + encodeURIComponent(query);
console.log(url);

Decode the matching format Top ↑

const encoded = encodeURIComponent("A&B");
console.log(decodeURIComponent(encoded)); // A&B

Encoding is not sanitization Top ↑

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




Subscribe to our YouTube Channel here



plus2net.com







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.



We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer