URL.canParse / / No Comments Parsing of URLs on the client side has been a common practice for two decades. The early days included using illegible regular expressions but the JavaScript specification eventually evolved into a new URL method of parsing URLs. While URL is incredibly useful when a valid URL is provided, an invalid string will throw an error — yikes! A new method, URL.canParse, will soon be available to validate URLs! Providing a malformed URL to new URL will throw an error, so every use of new URL would need to be within a try/catch block: // The correct, safest way try { const url = new URL('https://davidwalsh.name/pornhub-interview'); } catch (e) { console.log("Bad URL provided!"); } // Oops,... more → Posted in: JavaScript Tagged with: URL.canParse