encodeURI() functions to encode a string as a URL-encoded string, which can be used to represent special HTML characters.
var specialChar = "&"; var encodedChar = escape(specialChar); console.log(encodedChar); // %26
You can also use the
unescape() functions to decode a URL-encoded string back into its original form:
var encodedChar = "%26"; var specialChar = unescape(encodedChar); console.log(specialChar); // &
escape() is an older function, while
encodeURI() is more often used as it does not encode certain characters, such as
@, which are necessary for a valid URL.