encodeURI
Encodes a URI by replacing certain characters with UTF-8 escape sequences, but preserves characters that are part of URI syntax
Syntax
encodeURI(uri)Parameters
| Parameter | Type | Description |
|---|---|---|
| uri | string | A complete URI to encode |
Return Value
A new string representing the encoded URI
Examples
const uri = 'https://example.com/path with spaces?q=hello world';
console.log(encodeURI(uri));
// 'https://example.com/path%20with%20spaces?q=hello%20world'// encodeURI preserves :, /, ?, #, &, =
console.log(encodeURI('https://example.com/api?a=1&b=2'));
// 'https://example.com/api?a=1&b=2' (unchanged)// Use encodeURIComponent for individual values:
console.log(encodeURI('a=1&b=2')); // 'a=1&b=2'
console.log(encodeURIComponent('a=1&b=2')); // 'a%3D1%26b%3D2'Understanding encodeURI
The encodeURI method in JavaScript encodes a URI by replacing certain characters with UTF-8 escape sequences, but preserves characters that are part of URI syntax. It belongs to the window object and is one of the most widely used methods for working with window values in modern JavaScript and TypeScript applications.
The method signature is encodeURI(uri). It accepts 1 parameter: uri. When called, it returns a new string representing the encoded uri. Understanding when and how to use encodeURI() helps you write more expressive, readable code.
Common use cases for encodeURI include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like window-decodeuri, window-encodeuricomponent, window-decodeuricomponent, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for encodeURI is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
decodeURIDecodes a Uniform Resource Identifier previously created by encodeURI or by a similar routine
encodeURIComponentEncodes a URI component by replacing certain characters with UTF-8 escape sequences
decodeURIComponentDecodes a URI component previously created by encodeURIComponent or by a similar routine
More Global / Window Methods
Other methods in the Global / Window object
Related Tools
More Global / Window Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.