encodeURI()
The global encodeURI() function percent-encodes a complete URI, escaping characters such as spaces and accented letters while leaving the delimiters that give a URI its structure, such as /, ?, &, and #, unchanged. Each escaped character becomes one to four %XX sequences, one for each byte of its UTF-8 encoding:
> encodeURI("https://example.com/docs/café menu.html?lang=fr&page=2#top")
'https://example.com/docs/caf%C3%A9%20menu.html?lang=fr&page=2#top'
encodeURI() is an ECMAScript built-in, so it works the same in browsers and server runtimes. It uses the same algorithm as encodeURIComponent() but keeps URI delimiters as they are, because it expects a whole URI. Its inverse is decodeURI().
Syntax
The encodeURI() function takes one argument:
encodeURI(<uri>)
| Parameter | Description |
|---|---|
uri |
The URI to encode. A non-string value is converted to a string first, so a missing argument becomes "undefined" |
The function isn’t a constructor, so calling it with new throws a TypeError.
Return Value
The result is a new string. ASCII letters, digits, and - _ . ! ~ * ' ( ) are copied unchanged, and so are the delimiters ; / ? : @ & = + $ , #.
Every other character, including the space and %, becomes one to four %XX escape sequences written with uppercase hexadecimal digits. A lone surrogate, which is half of a UTF-16 surrogate pair with no partner, has no UTF-8 encoding. When the string contains one, the call throws a URIError instead of returning.
Examples
A wiki builds page links from titles written in any language. Encoding the finished URL escapes the non-ASCII title while the slashes keep separating the path segments:
> encodeURI("https://example.com/wiki/文档/")
'https://example.com/wiki/%E6%96%87%E6%A1%A3/'
A logging tool stores encoded URLs and shows them in readable form on its dashboard. The decodeURI() function reverses the encoding, so the round trip returns the original text:
> const address = encodeURI("https://example.com/café?table=4");
undefined
> address
'https://example.com/caf%C3%A9?table=4'
> decodeURI(address)
'https://example.com/café?table=4'
Common Gotchas
Delimiters inside a value stay unescaped too. A search link built from user input breaks when the input contains &, because the URL parser then sees two query parameters:
> const name = "Ben & Jerry";
undefined
> const link = encodeURI(`https://example.com/search?q=${name}`);
undefined
> new URL(link).searchParams.get("q")
'Ben '
> `https://example.com/search?q=${encodeURIComponent(name)}`
'https://example.com/search?q=Ben%20%26%20Jerry'
Encoding each value with encodeURIComponent() keeps the & inside the parameter.
The % sign is escaped as well, so a URL that’s already encoded gets encoded twice:
> encodeURI("https://example.com/report%202026.pdf")
'https://example.com/report%25202026.pdf'
The unescaped set comes from RFC 2396, written before URIs could hold IPv6 addresses. RFC 3986 reserves [ and ] for IPv6 host addresses, but encodeURI() escapes them and breaks the host:
> encodeURI("http://[::1]:8080/status")
'http://%5B::1%5D:8080/status'
Related Resources
Course
JavaScript Strings for Pythonistas
Port a payments team's branch-name helper to JavaScript, and learn where Python's string habits, from slicing to split() and join(), run without an error and give the wrong text.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 15, 2026