Saturday, March 23, 2024

The JavaScript escape() Operate

Must read

Ever come throughout the JavaScript escape() perform and marvel what it is all about? On this article, we’ll dive into this lesser-used, however often useful JavaScript perform. The escape() perform just isn’t often within the highlight relating to JavaScript improvement, but it surely does turn out to be useful when it is advisable to encode particular characters in a string.

Notice: The escape() perform in JavaScript is deprecated and never beneficial to be used in fashionable net improvement. Nevertheless, understanding its utilization may assist in sustaining or refactoring older codebases that also put it to use.

As you may see later on this article, we suggest you employ encodeURIComponent() as an alternative.

What’s the escape() Operate?

The escape() perform in JavaScript is used to encode a string. This perform makes a string transportable in order that it may be transmitted throughout any community to any pc that helps ASCII characters. When encoding, it takes a string and replaces sure characters with escape sequences.

let str = "Hiya, World!";
let consequence = escape(str);
console.log(consequence);  // Outputs: Hellopercent2Cpercent20Worldpercent21

Within the code above, the escape() perform replaces the comma (,) and exclamation mark (!) with %2C and %20, respectively.

Notice: The escape sequences are initiated by a ‘%’ signal, adopted by two hexadecimal digits representing the ASCII worth of the character.

When to Use escape()

Regardless of its deprecation, you would possibly come throughout the escape() perform in older JavaScript code, notably when coping with non-ASCII characters, resembling these in URLs or cookies. The escape() perform is helpful when it is advisable to make sure that these characters are transmitted accurately throughout the community.

let url = "";
let consequence = escape(url);
console.log(consequence);  // Outputs: httppercent3A//

On this instance, the escape() perform encodes the URL by changing sure characters with their corresponding escape sequences.

Limitations and Options

Whereas the escape() perform may be helpful in some conditions, it undoubtedly has limitations. For instance, it doesn’t encode characters like +, @, and /, which might trigger points in sure circumstances, resembling when coping with internationalized domains, emails, or URLs.

A extra appropriate various to the escape() perform is encodeURIComponent(), which encodes particular characters, together with these ignored by the escape() perform.

let url = "";
let consequence = encodeURIComponent(url);
console.log(consequence);  // Outputs: httppercent3Apercent2Fpercent2Fexample.compercent2Fpercent3Fnamepercent3DJohnpercent26agepercent3D20

The encodeURIComponent() perform encodes the ahead slash (/), whereas the escape() perform doesn’t.


Regardless that the JavaScript escape() perform is now deprecated, it has had its makes use of previously, notably when coping with particular characters in strings. Understanding the way it works may be useful when coping with older codebases. Nevertheless, for up to date improvement duties, particularly these involving URLs or Unicode characters, alternate options like encodeURIComponent() supply a extra complete and dependable resolution.

Supply hyperlink

More articles


Please enter your comment!
Please enter your name here

Latest article