URL Encoding Explained: A Practical Guide with Examples
A URL may look like an ordinary web address, but not every character can be used in it exactly as written. Spaces, special symbols, and certain characters can create problems when a browser, server, or API tries to interpret a URL.
This is where URL encoding becomes important.
URL encoding converts characters into a format that can be safely included in a URL. It is commonly used when passing search terms, user input, API parameters, and tracking information through web addresses.
For example, a space in a URL may be represented as %20. The original text:
web development tools
can become:
web%20development%20tools
Understanding this process helps prevent broken links, unexpected API responses, and incorrectly interpreted query parameters.
In this guide, you will learn how URL encoding works, which characters need encoding, how to encode and decode URLs in JavaScript, and when to use an online URL encoder.
What Is URL Encoding?
URL encoding, also called percent encoding, is a method of representing characters using a percent sign (%) followed by two hexadecimal digits.
A URL contains different components, including a scheme, domain, path, query string, and sometimes a fragment. Certain characters have special meanings within these components. If a character is used as ordinary data but interpreted as URL syntax, the resulting address may not behave as expected.
URL encoding helps represent those characters safely.
For example:
| Original character | Encoded representation |
|---|---|
| Space | %20 |
& | %26 |
= | %3D |
? | %3F |
# | %23 |
+ | %2B |
/ | %2F |
The encoding rules are defined in the URI syntax standard, RFC 3986.
One important detail: URL encoding does not mean converting an entire URL into unreadable text. It means encoding the characters that need to be represented as data within a particular URL component.
Why Is URL Encoding Important?
URL encoding matters whenever information is transferred through a web address.
Consider a search form that sends a user’s search term to a website. Someone searches for:
best web development tools
If the application places this value into a URL without handling its spaces and other reserved characters correctly, the resulting address may not represent the intended value.
The same issue can occur with API requests, product filters, redirect URLs, and campaign tracking links.
Here are some common situations where URL encoding is useful.
1. Passing Search Queries Through URLs
Search terms often contain spaces, punctuation, or special characters.
For example, a search URL might look like this:
The encoded value is passed as a query parameter, allowing the application to retrieve the intended search term.
2. Sending Data to APIs
APIs frequently accept information through query parameters.
For example:
Here, the apostrophe and spaces are represented in encoded form.
When an application constructs an API request using user input, encoding the parameter value helps prevent characters from being mistaken for URL syntax.
3. Creating Tracking URLs
Marketing campaigns often use URL parameters to identify traffic sources, campaigns, and other details.
For example, a campaign name might contain spaces or special characters. Those values should be encoded appropriately when added to a tracking URL.
You can create campaign links using the UTM Builder. For a deeper explanation, read How to Build UTM Tracking URLs: A Step-by-Step Guide.
4. Working with Redirects and Dynamic Links
Web applications sometimes pass destination URLs as parameter values.
A destination URL may contain its own query string, symbols, and separators. If it is inserted into another URL without proper encoding, its characters can interfere with the outer URL’s structure.
Encoding the destination value correctly helps preserve its intended meaning.
How Does URL Encoding Work?
URL encoding represents a character using its byte value in hexadecimal notation, preceded by %.
For example, the character A has the hexadecimal byte value 41 in ASCII and UTF-8. The character A does not need percent encoding in a normal URL component, but a space is commonly represented as %20.
For text containing non-ASCII characters, UTF-8 bytes are percent-encoded individually.
For example:
café
Its UTF-8 representation includes the bytes C3 A9 for é, so the encoded form is:
caf%C3%A9
This is why URL encoding is more than simply replacing spaces with symbols.
Reserved and Unreserved Characters
URL characters can be understood in two broad groups.
Unreserved characters include:
- Letters:
A-Zanda-z - Numbers:
0-9 - Hyphen:
- - Period:
. - Underscore:
_ - Tilde:
~
These characters can generally appear without percent encoding.
Reserved characters have structural meaning in URLs. Examples include:
?for the beginning of a query&for separating query parameters=for separating parameter names and values#for a fragment identifier/for separating path segments
Whether a reserved character needs encoding depends on where it appears and whether it is intended as URL syntax or ordinary data.
For a deeper technical explanation, see the URI Generic Syntax specification.
How to Encode a URL Online
You do not need to write code every time you need to encode a URL value.
An online URL encoder can convert text into percent-encoded form and help you inspect the result before using it in a link or API request.
You can use the URL Encoder and Decoder tool on The Awais Ali.
A typical workflow is:
- Open the URL Encoder and Decoder tool.
- Enter the text or URL component you want to encode.
- Run the encoding operation.
- Copy the encoded result.
- Use it in the appropriate URL component or request parameter.
For example, enter:
product search & filters
The encoded result for a query parameter value is:
product%20search%20%26%20filters
Notice that the ampersand becomes %26. This is important because an unencoded ampersand is normally used to separate query parameters.
Always encode the correct component rather than blindly encoding an entire URL.
How to Encode and Decode URLs in JavaScript
JavaScript provides built-in functions for URL encoding and decoding. You do not need an additional library for common encoding tasks.
However, choosing the correct function matters.
1. Use encodeURIComponent() for Individual Values
encodeURIComponent() is useful when you need to encode a query parameter value or another individual URL component.
Example:
const searchTerm = "web development & design";
const encodedTerm = encodeURIComponent(searchTerm);
console.log(encodedTerm);
Output:
web%20development%20%26%20design
You can then use the encoded value in a URL:
const searchTerm = "web development & design";
const url = `https://example.com/search?q=${encodeURIComponent(searchTerm)}`;
console.log(url);
This produces:
https://example.com/search?q=web%20development%20%26%20design
The ampersand is encoded because it is part of the search term, not a separator between query parameters.
2. Use decodeURIComponent() to Decode Values
If you receive an encoded parameter and need its original text, use decodeURIComponent().
const encodedTerm = "web%20development%20%26%20design";
const originalTerm = decodeURIComponent(encodedTerm);
console.log(originalTerm);
Output:
web development & design
This is useful when processing query parameters or reading encoded values received from another application.
3. Use URLSearchParams for Query Parameters
When you need to create or manage multiple query parameters, JavaScript’s URLSearchParams is often a better choice.
const params = new URLSearchParams();
params.set("search", "web development");
params.set("category", "tools");
params.set("sort", "popular");
const url = `https://example.com/products?${params.toString()}`;
console.log(url);
This generates a query string with the values encoded appropriately.
You can also read parameters:
const url = new URL(
"https://example.com/products?search=web%20development&category=tools"
);
const search = url.searchParams.get("search");
const category = url.searchParams.get("category");
console.log(search);
console.log(category);
For more details, see the official MDN documentation for encodeURIComponent() and URLSearchParams.
encodeURI() vs encodeURIComponent(): What Is the Difference?
JavaScript provides two commonly used encoding functions, but they serve different purposes.
| Function | Main purpose |
|---|---|
encodeURI() | Encodes a complete URI while preserving characters used to define its structure |
encodeURIComponent() | Encodes an individual component, such as a query parameter value |
For example:
const fullURL = "https://example.com/search?q=web development";
console.log(encodeURI(fullURL));
encodeURI() is intended for a complete URI. It preserves structural characters such as /, ?, and &.
Now consider an individual value:
const value = "web development & design";
console.log(encodeURIComponent(value));
Output:
web%20development%20%26%20design
The ampersand is encoded because it is part of the value.
Which one should you use?
Use encodeURIComponent() when inserting a value into a query parameter. Use encodeURI() when you need to encode a complete URI while preserving its structural characters.
For most dynamic query parameters, encodeURIComponent() is the more appropriate choice.
Common URL Encoding Mistakes to Avoid
Even experienced developers can run into URL encoding problems when handling user input or constructing API requests.
Here are some mistakes worth avoiding.
1. Encoding the Entire URL Instead of Its Parameter Value
Encoding a complete URL with encodeURIComponent() changes structural characters such as :, /, and ?.
That may be correct when the entire URL is being passed as a value inside another URL, but it is not appropriate when you simply want to construct a normal web address.
Encode the component that needs encoding.
2. Encoding the Same Value Multiple Times
Double encoding happens when an already encoded value is encoded again.
For example:
Original: hello world
Encoded once: hello%20world
Encoded twice: hello%2520world
The % character becomes %25 during the second encoding.
This can cause an application to receive a different value than expected.
3. Confusing Plus Signs with Spaces
In standard percent encoding, a space is represented as %20.
However, form-style query serialization using application/x-www-form-urlencoded commonly represents spaces with +.
For example:
web+development
may represent:
web development
But a literal plus sign is represented as %2B.
This distinction matters when working with query strings, forms, and APIs.
4. Ignoring Invalid Encoded Sequences
Decoding an invalid percent-encoded sequence with decodeURIComponent() can throw a URIError.
For example:
try {
const value = decodeURIComponent("%E0%A4%A");
console.log(value);
} catch (error) {
console.error("Invalid encoded value:", error.message);
}
Use error handling when decoding data that comes from an external source.
5. Assuming URL Encoding Fixes Every URL Problem
Encoding helps represent data safely, but it does not automatically fix incorrect URLs, missing parameters, server permissions, or broken API endpoints.
If a request still fails, investigate the actual HTTP response and request configuration. You can also read HTTP 401, 403, 404, and 500 Errors Explained to understand common server-side and request-related problems.
When Should You Use a URL Encoder?
A URL encoder is useful whenever you need to convert text into a URL-safe representation before placing it into a particular URL component.
Common use cases include:
- Preparing search terms for query parameters.
- Constructing API requests with dynamic values.
- Creating campaign and UTM tracking links.
- Passing destination URLs through redirect parameters.
- Testing special characters in web applications.
- Debugging incorrectly formatted query strings.
- Converting encoded text back into readable form.
For everyday tasks, an online encoder and decoder can save time. For application development, JavaScript functions and URL APIs provide more control.
Frequently Asked Questions
What is URL encoding in simple terms?
URL encoding is the process of representing certain characters in a URL using a percent sign followed by hexadecimal digits. It helps browsers and servers interpret URL data correctly.
What does %20 mean in a URL?
%20 represents a space character in percent encoding. For example, web%20development represents web development.
Is URL encoding the same as Base64 encoding?
No. URL encoding and Base64 are different encoding methods. URL encoding represents characters in a URL-compatible format, while Base64 represents binary data using a set of 64 characters. They serve different purposes.
Does every URL need encoding?
No. Many URLs already contain characters that are valid in their intended positions. Encoding is needed when characters must be represented as data within a URL component, particularly when handling dynamic values.
Is encodeURI() or encodeURIComponent() better?
Neither is universally better. encodeURI() is designed for complete URIs, while encodeURIComponent() is designed for individual components such as query parameter values. Choose based on what you are encoding.
How do I decode a URL?
In JavaScript, use decodeURIComponent() to decode an individual URL component. You can also use an online URL decoder to convert encoded text into a readable form.
Why does a URL contain %25?
%25 represents the percent sign itself. It commonly appears when a percent-encoded value has been encoded again, although it can also be intentional when the original data contains a literal percent sign.
Can URL encoding improve SEO?
URL encoding is not a direct SEO ranking technique. Search engines can process valid URLs containing percent-encoded characters. However, readable, descriptive URL structures can make links easier for people to understand and manage. See Google’s guidance on URL structure.
Final Thoughts
URL encoding is a small part of web development, but it affects many everyday tasks: search forms, API requests, redirects, campaign links, and dynamic URLs.
The most important thing is to understand which part of a URL you are encoding. Use encodeURIComponent() for individual values, URLSearchParams for query strings, and avoid repeatedly encoding values that are already encoded.
If you need to encode or decode text without writing JavaScript, try the URL Encoder and Decoder tool on The Awais Ali.





