Today I Learned - Rocky Kev

TIL check if the URL is valid

POSTED ON:

TAGS:

Brilliant! This uses the web APIs URL() - MDN.

const isValidURL = (url) => {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
};

isValidURL("https://dev.to");
// true

isValidURL("https//invalidto");
// false

new URL('/en-US/docs'); // Raises a TypeError exception as '/en-US/docs' is not a valid URL
new URL('http://www.example.com', ); // => 'http://www.example.com/'

https://caniuse.com/url

Via 11 JavaScript Tips and Tricks to Code Like A Superhero (Vol.2)](https://dev.to/orkhanjafarovr/11-javascript-tips-and-tricks-to-code-like-a-superhero-vol-2-mp6)


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.