encodeURIComponent()
The global encodeURIComponent() function percent-encodes a string so it can sit safely inside one part of a URI, such as a query parameter value or a path segment. It escapes every character except ASCII letters, digits, and - _ . ! ~ * ' ( ), so delimiters like &, /, and ? lose their special meaning:
> encodeURIComponent("Jack & Jill")
'Jack%20%26%20Jill'
> encodeURIComponent("café/menu?")
'caf%C3%A9%2Fmenu%3F'
encodeURIComponent() is an ECMAScript built-in, so it works the same in browsers and server runtimes. Its inverse is decodeURIComponent(). The related encodeURI() function uses the same algorithm but leaves URI delimiters unescaped, because it expects a whole URI.
Syntax
The encodeURIComponent() function takes one argument:
encodeURIComponent(<uriComponent>)
| Parameter | Description |
|---|---|
uriComponent |
The text to encode. A non-string value is converted to a string first, so 42 becomes "42" and 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. Characters in the unescaped set are copied unchanged. Every other character becomes one to four %XX escape sequences, one for each byte of its UTF-8 encoding, written with uppercase hexadecimal digits.
Type a string or pick an example to see what happens to each of its characters:
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 search page builds a link from what a user typed. Without encoding, the & would start a new query parameter and the spaces would make the URL invalid:
> const query = "rock & roll";
undefined
> `https://example.com/search?q=${query}`
'https://example.com/search?q=rock & roll'
> `https://example.com/search?q=${encodeURIComponent(query)}`
'https://example.com/search?q=rock%20%26%20roll'
A file-sharing app puts file names into URL paths. Encoding the name keeps a / inside it from creating an extra path segment, and the accented letters become UTF-8 escapes:
> const fileName = "résumé/v2.pdf";
undefined
> `/files/${encodeURIComponent(fileName)}`
'/files/r%C3%A9sum%C3%A9%2Fv2.pdf'
Common Gotchas
The % sign is escaped too, so encoding a value twice turns each escape into %25 followed by its hex digits:
> const label = encodeURIComponent("50% off");
undefined
> label
'50%25%20off'
> encodeURIComponent(label)
'50%2525%2520off'
A server that decodes the doubly encoded value once receives 50%25%20off instead of 50% off.
A string with a lone surrogate throws, which can happen after slicing text in the middle of an emoji. The .toWellFormed() method, added in ES2024, replaces lone surrogates with U+FFFD, the replacement character:
> const truncated = "👍".slice(0, 1);
undefined
> encodeURIComponent(truncated)
Uncaught URIError: URI malformed
at encodeURIComponent (<anonymous>)
> encodeURIComponent(truncated.toWellFormed())
'%EF%BF%BD'
The unescaped set comes from RFC 2396, so !, ', (, ), and * stay as they are even though the newer RFC 3986 reserves them. Spaces become %20, while HTML form encoding, which URLSearchParams produces, uses +:
> encodeURIComponent("it's (really) great!*")
"it's%20(really)%20great!*"
> new URLSearchParams({ q: "rock & roll" }).toString()
'q=rock+%26+roll'
The deprecated escape() function isn’t a drop-in replacement. It doesn’t use UTF-8, so non-ASCII characters come out as %XX or %uXXXX sequences that decodeURIComponent() can’t read.
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. 14, 2026