You can encode or decode a URL in JavaScript using the built-in encodeURIComponent and decodeURIComponent functions. These functions will replace any special characters in the URL with their percent-encoded representation and vice versa.