URL encoder/decoder · Guide

URL Encoder JavaScript: encodeURIComponent vs encodeURI

JavaScript ships four ways to URL-encode, and three of them are wrong for the job you probably have. Here is which to reach for, what each leaves unencoded, and why URLSearchParams usually beats doing it by hand.

encodeURIComponent — for values

This is the one you want for a single query parameter value, a path segment, or anything going inside a URL as data. It encodes reserved characters including & = ? # / : @ +.

It deliberately leaves A-Z a-z 0-9 - _ . ! ~ * ' ( ) alone. If you are signing a request for an API that expects RFC 3986 strictness, encode the last five yourself:

JavaScript
const strict = (s) =>
  encodeURIComponent(s).replace(
    /[!'()*]/g,
    (c) => '%' + c.charCodeAt(0).toString(16).toUpperCase()
  );

encodeURI — for whole URLs only

encodeURI assumes it is given a complete URL and preserves the characters that give a URL structure: : / ? # & = @ + $ , ;. It only escapes genuinely unsafe characters such as spaces and non-ASCII.

That makes it useless for encoding a value:

JavaScript
encodeURI('a&b');          // 'a&b'   -> splits into two parameters
encodeURIComponent('a&b'); // 'a%26b' -> stays one value

Using encodeURI where encodeURIComponent belongs is the single most common URL bug in JavaScript.

escape — never use it

escape() is deprecated and was never UTF-8 aware. It encodes non-ASCII as %uXXXX, which is not valid percent-encoding and no server decodes correctly. It also leaves + and / unescaped.

If you find it in a codebase, replacing it with encodeURIComponent is almost always the correct fix.

URLSearchParams and URL — the modern way

Instead of concatenating and encoding by hand, build the query with URLSearchParams — every value is encoded for you:

JavaScript
const p = new URLSearchParams({ q: 'coffee & tea', page: '2' });
p.toString(); // 'q=coffee+%26+tea&page=2'

Note it encodes spaces as +, following form rules. If you need %20, use new URL() and set url.searchParams, or replace /\+/g with %20 on the final string.

For decoding, decodeURIComponent reverses encodeURIComponent. It throws a URIError on malformed input such as a lone % — wrap it in try/catch when the input comes from users.

Frequently asked questions

encodeURI or encodeURIComponent?

encodeURIComponent for a single value; encodeURI only when you have a full URL that is already structurally correct and just needs unsafe characters escaped.

Why does URLSearchParams use + for spaces?

It follows the application/x-www-form-urlencoded rules, where + means space. Most servers decode it correctly; convert to %20 if your receiver is strict.

Why does decodeURIComponent throw?

The input contains a malformed escape, such as a % not followed by two hex digits. Validate or catch the URIError before using the result.

Ready to try it?

Open the free browser-based URL encoder/decoder and apply what you just read — no sign-up, runs locally.

Open the URL encoder/decoder tool