Favorites

JavaScript encodeURIComponent() Function

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
encodeURIComponent()
JavaScript Global functionThis function is supported by all modern browsers.
Updated 28th November 2026

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

const query = 'cats & dogs?';
console.log(encodeURIComponent(query));
console.log('/search?q=' + encodeURIComponent(query));
console.log(encodeURIComponent('café'));

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.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee