Developer

encodeURI vs encodeURIComponent: Which One to Use

ToolOrbit Engineering 2 min readUpdated
encodeURI vs encodeURIComponent: Which One to Use

URLs can only contain a limited set of characters. Spaces, ampersands, question marks, and non-ASCII letters all have special meaning or are simply not allowed. Percent-encoding, often called URL encoding, solves this by replacing unsafe characters with a % followed by two hexadecimal digits. The ToolOrbit URL Encode / Decode tool makes this reliable and lets you choose the right scope for the job.

How percent-encoding works

Each byte that is not allowed in a URL is converted to its hexadecimal value preceded by a percent sign. A space becomes %20, an ampersand becomes %26, and a non-ASCII character is first encoded as UTF-8 bytes, each of which is then percent-encoded. Decoding simply reverses the process.

hello world & friends
->
hello%20world%20%26%20friends

cafe (UTF-8 accent) -> ca%C3%A9

Component vs full URL encoding

This is the single most important distinction. When you encode a value that will sit inside a query parameter, you must encode reserved characters like &, =, ?, and / so they do not change the structure of the URL. When you have an already-assembled URL and only want to fix spaces and illegal characters, you must leave those structural characters intact.

  • Component mode (like encodeURIComponent): use for individual query values, path segments, or form fields
  • Full URL mode (like encodeURI): use for an entire URL where the structure is already correct
  • Always encode user input as a component before stitching it into a query string
  • Never double-encode: encoding an already-encoded string turns % into %25
Warning: Encoding a whole URL when you meant to encode just one parameter is the top cause of broken links. If your & separators vanish, you used the wrong mode.

Using the tool

Paste your text, pick component or full URL mode, and switch between encode and decode. Everything happens locally in your browser, so internal URLs and tokens never leave your machine. Decode mode is handy for reading the real values behind a long, percent-heavy analytics link.

Best practices

Encode early and decode late: encode each value the moment you place it into a URL, and decode only when you read it back. When in doubt, treat every dynamic value as a component. This keeps your URLs valid, your query parameters intact, and your redirects working as intended.

The line between encode(whole URL) and encode(part)

In JavaScript, encodeURI() leaves structural characters (/, :, ?, &, =) alone so a whole URL survives; encodeURIComponent() encodes even those, so a single value is safe to embed in an already-valid URL. Getting this backwards is the classic “why is my query broken” bug: encode the path using the first, encode values using the second. Test the finished link in the tool before shipping it — a percent-encoded URL that fails pasting into a client is still failure.

Reserved-looking text that must reach the API literally (like “50% off” or a plus sign in a search term) has to be encoded — an unencoded % or + is interpreted, not transmitted.

Tools mentioned

More reading

View all guides
What Is Base64? How Encoding Works and Common Mistakes
Developer

What Is Base64? How Encoding Works and Common Mistakes

What Base64 encoding actually does, where it is used, and the mistakes that cause broken or oversized output.

2 min readUpdated
What Is a UUID? Version 4 vs Version 7 Explained
Developer

What Is a UUID? Version 4 vs Version 7 Explained

What a UUID looks like, why version 4 is the safe default, when version 7 is better, and best practices for IDs.

2 min readUpdated
How SHA-256 and SHA-512 Hashes Work: Hashing vs Encryption
Developer

How SHA-256 and SHA-512 Hashes Work: Hashing vs Encryption

What a hash function does, how to choose between SHA-256 and SHA-512, and why hashing is not encryption.

2 min readUpdated
What Is a JWT? Header, Payload and Signature Explained
Developer

What Is a JWT? Header, Payload and Signature Explained

A JSON Web Token has three parts. Learn what the header, payload and signature contain, how to read the claims, and why decoding is not verifying.

2 min readUpdated
What Is HTML Encoding? Entities & Escaping Explained
Developer

What Is HTML Encoding? Entities & Escaping Explained

Learn how HTML encoding works, why escaping prevents XSS and layout breaks, and how to encode and decode named, decimal, and hex entities.

2 min readUpdated
Unix Time Explained: Seconds, Milliseconds and Time Zones
Developer

Unix Time Explained: Seconds, Milliseconds and Time Zones

What a Unix timestamp is, how to tell seconds from milliseconds, how time zones come into it, and the values worth recognising.

2 min readUpdated
Binary, Octal, Decimal and Hex: How Number Bases Work
Developer

Binary, Octal, Decimal and Hex: How Number Bases Work

How positional number bases work, why very large values need exact arithmetic, and the hex and binary values you meet every day.

2 min readUpdated
Regex Basics: How Patterns, Flags and Character Classes Work
Developer

Regex Basics: How Patterns, Flags and Character Classes Work

How a regular expression is built, what the flags change, and the character classes you will use most.

2 min readUpdated