JavaScript encodeURIComponent() Function
The encodeURIComponent() function encodes text so it can be safely included in part of a URL, such as a query string value.
Description
The encodeURIComponent() function percent-encodes a string so it can be used safely as one component of a URL, such as a search query or path segment. It encodes everything except letters, digits and - _ . ! ~ * ' ( ), including &, =, ? and /.
Use it for values you put into URLs. To encode a whole URL without breaking its structure, use encodeURI(). The URL and URLSearchParams objects handle encoding for you.
It encodes text as UTF-8 before percent-encoding, so non-ASCII characters become several escape sequences, as with "é" becoming %C3%A9. Spaces become %20, not +; HTML forms use + for spaces in query strings, which URLSearchParams produces automatically.
Syntax
encodeURIComponent(string)Parameters
- stringThe text to encode.
Return Value
A new string with special characters replaced by percent-encoded escape sequences.
Exceptions
- URIErrorThrown if the string contains a lone surrogate character.
Example
Browser Support
The following information will show you the current browser support for the encodeURIComponent() function. Hover over a browser icon to see the version that first introduced support for this function.
Desktop





Tablets & Mobile





