JavaScript (ES2026) reference · JSON and Global Functions
JavaScript encodeURIComponent() Function
Encode a string for use as one part of a URL (a query value or path segment), escaping &, =, ?, / and spaces.
JavaScript (ES2026) Since ES3
Syntax
encodeURIComponent(string)
Parameters
| Parameter | Description |
|---|---|
string |
The text to encode. |
Return value
An encoded string.
Examples
const q = "tea & cake?";
console.log(`https://example.com/search?q=${encodeURIComponent(q)}`);
Output
https://example.com/search?q=tea%20%26%20cake%3F
console.log(new URLSearchParams({ q: "tea & cake?", page: 2 }).toString());
Output
q=tea+%26+cake%3F&page=2
Version history
| Version | Change |
|---|---|
| JavaScript ES3 | Added |
In other languages
urlencode()· PHPrawurlencode()· PHPhttp_build_query()· PHP
Related
decodeURIComponent()· Decode a URL component encoded by encodeURIComponent(), turning %20-style escapes back into characters.encodeURI()· Encode a complete URL, leaving characters with special meaning in URLs (like :, /, ? and &) untouched.- JavaScript Fetch API · tutorial