How to Use HTML Entities in JavaScript?

Estimated read time 1 min read

In JavaScript, you can use the escape() or encodeURI() functions to encode a string as a URL-encoded string, which can be used to represent special HTML characters.

For example:

var specialChar = "&";
var encodedChar = escape(specialChar);
console.log(encodedChar); // %26

You can also use the decodeURI() or unescape() functions to decode a URL-encoded string back into its original form:

var encodedChar = "%26";
var specialChar = unescape(encodedChar);
console.log(specialChar); // &

Note: escape() is an older function, while encodeURI() is more often used as it does not encode certain characters, such as /, :, and @, which are necessary for a valid URL.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply