What is difference between unescape() and escape() functions in JavaScript?

JavaScript provides two legacy functions for dealing with encoded strings: escape() and unescape(). The escape() function encodes a string, making certain characters safe for URLs, while unescape() decodes an encoded string back to its original form.

?? Important: Both escape() and unescape() are deprecated. Use encodeURIComponent()/decodeURIComponent() or encodeURI()/decodeURI() instead.

Syntax

escape(string)
unescape(encodedString)

Key Differences

Function Purpose What it does
escape() Encodes special characters Converts characters like spaces, punctuation to %XX format
unescape() Decodes encoded characters Converts %XX sequences back to original characters

Example: Basic Usage



    
    
    
    
    


Characters That Get Encoded

The escape() function encodes characters that are not alphanumeric or one of: * + - . / @ _



    
    
    


Modern Alternatives (Recommended)

Instead of deprecated escape() and unescape(), use these modern functions:



    
    
    


Why They're Deprecated

  • Limited character support: Only handles ASCII characters properly

  • Security concerns: Can lead to vulnerabilities when handling untrusted input

  • Browser inconsistencies: Not supported in all modern browsers

  • Better alternatives exist: URI encoding functions are more reliable and standards-compliant

Conclusion

While escape() and unescape() were once used for URL encoding, they're now deprecated. Use encodeURIComponent()/decodeURIComponent() for encoding URL parameters or encodeURI()/decodeURI() for full URLs instead.

Updated on: 2026-03-15T23:19:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements