In JavaScript, you can encode and decode a URL using the following built-in functions:
encodeURI()
: Encodes a full URL but does not encode special characters like :
, /
, ?
, &
, =
, etc.
encodeURIComponent()
: Encodes a URL component (query parameters, path segments). It encodes special characters like :
, /
, ?
, &
, =
, etc.
decodeURI()
: Decodes a full URL encoded with encodeURI()
.
decodeURIComponent()
: Decodes a URL component encoded with encodeURIComponent()
.
encodeURI
/ decodeURI
when handling an entire URL.encodeURIComponent
/ decodeURIComponent
when encoding or decoding query parameters or specific parts of a URL.
Would you like a real-world use case, such as working with URLSearchParams
? 🚀