URL Encoding in JavaScript

An online tool, a plain-English explanation and copy-ready code examples for percent (%XX) encoding with the browser's own encodeURIComponent / encodeURI / decodeURIComponent. Everything runs in your browser.

← Back to the URL Encoder

Everything on this page runs in your browser — your text is never uploaded to a server. The code examples are ready to copy into your own project.

① Online tool: watch JavaScript encode as you type

Type below and the three boxes underneath update live with the result of each of JavaScript's three native functions.

Function
Target
encodeURIComponent(input)
encodeURI(input)
decodeURIComponent(input)

② Plain English: how these functions actually differ

Diagram separating a URL into component values and delimiters before encoding.
Use component encoding for data values; keep the URL structure intact.

③ Code examples (hit Copy and paste them in)

1) Basic encoding: one parameter value
// encode a value so it can be used as a query parameter
const q = encodeURIComponent('café & tea');
// → "caf%C3%A9%20%26%20tea"
const url = 'https://example.com/search?q=' + q;
2) Building a query string safely
function buildQuery(params) {
  return Object.keys(params)
    .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
    .join('&');
}
const qs = buildQuery({ q: 'café', page: 2 });
// → "q=caf%C3%A9&page=2"
3) Reading and decoding a parameter from the address bar (URLSearchParams)
// in a browser: decoded for you automatically
const params = new URLSearchParams(window.location.search);
const q = params.get('q');   // already the decoded original text

// decoding an encoded string by hand
const raw = 'caf%C3%A9';
const text = decodeURIComponent(raw);  // "café"
4) Encoding every part of a whole URL (the correct way)
function encodeUrl(url) {
  const u = new URL(url);
  u.pathname = u.pathname.split('/').map(encodeURIComponent).join('/');
  u.search = '?' + new URLSearchParams(
    [...new URLSearchParams(u.search)].map(
      ([k, v]) => [encodeURIComponent(k), encodeURIComponent(v)]
    )
  ).toString();
  return u.toString();
}
encodeUrl('https://example.com/café?q=café');
5) Preventing double-encoding
function safeEncode(str) {
  // if it already contains %XX, decode first so that we only encode once
  const decoded = /%[0-9A-Fa-f]{2}/.test(str) ? decodeURIComponent(str) : str;
  return encodeURIComponent(decoded);
}

Why does JavaScript URL encoding leave apostrophes unchanged?

encodeURIComponent() intentionally leaves a small set of punctuation characters, including the apostrophe, unescaped. If a receiver requires strict RFC 3986 percent-encoding for one component, encode those remaining characters after calling it:

function encodeStrictComponent(value) {
  return encodeURIComponent(value).replace(/[!'()*]/g, char =>
    "%" + char.charCodeAt(0).toString(16).toUpperCase()
  );
}

encodeStrictComponent("O'Reilly"); // "O%27Reilly"

Apply this to one query value or path segment, not to the complete URL. See the MDN encodeURIComponent reference and RFC 3986 reserved characters. If the value is going into HTML, JavaScript source, or a shell command, that output context may require its own escaping too; URL encoding does not replace context-specific escaping.

No sign-up · Unlimited use · Runs only in your browser. Want the general-purpose encoder instead? Go here →

Watch: encodeURI vs encodeURIComponent

Compare the two JavaScript URL encoders and learn when to encode one value or preserve a complete URL.

Watch on YouTube

Related tools

Every tool on this site runs entirely in your browser. Pick the encoding problem you have:

Getting URIError before a request is sent? Find lone surrogates and compare rejection with replacement.

Same-looking text producing different percent escapes? Compare Unicode NFC and NFD URL bytes before normalizing a value.

Working with repeated names? Compare ordered query pairs with scalar reads and object conversion before a value disappears.

Does an encoded dot change your route? Inspect dot-segment parsing and compare the actual request target.