DevToolsForYou

URL Encoder and Decoder in JavaScript / Node.js — Code Examples

URL Encoder and Decoder in JavaScript / Node.jsUse the online tool →

URL encoding (percent-encoding) replaces characters that are not allowed in URLs with a percent sign followed by their hexadecimal value. Here is how to encode and decode URL components in each language.

JavaScript has two pairs of functions: encodeURI/decodeURI for full URLs and encodeURIComponent/decodeURIComponent for individual query parameter values.

Browser and Node.js
// Encode a query parameter value (most common use case)
const param = "hello world & foo=bar";
const encoded = encodeURIComponent(param);
console.log(encoded); // hello%20world%20%26%20foo%3Dbar

// Decode
const decoded = decodeURIComponent("hello%20world%20%26%20foo%3Dbar");
console.log(decoded); // hello world & foo=bar

// Encode a full URL (preserves ://?=& structure)
const url = "https://example.com/search?q=hello world";
const encodedUrl = encodeURI(url);
console.log(encodedUrl); // https://example.com/search?q=hello%20world

// Build a query string from an object
const params = new URLSearchParams({ q: "hello world", page: "1" });
console.log(params.toString()); // q=hello+world&page=1
console.log("?" + params.toString());
Notes & gotchas
  • Use encodeURIComponent for query parameter values, not encodeURI — encodeURI leaves & = ? / intact.
  • URLSearchParams is the modern way to build query strings; it handles encoding automatically.
  • encodeURIComponent encodes spaces as %20; URLSearchParams encodes them as + (both are valid in query strings).
Try it in your browser

Need to url encode/decode without writing code? The URL Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open URL Encode/Decode
URL Encoder and Decoder in other languages