Switch language한국어
Back to the list

You've been wrapping `new URL()` in try/catch. `URL.parse()` does it natively.

TL;DR AI

Key summary

2 min read
  1. Modern browsers now support `URL.canParse()` and `URL.parse()`, reducing the need to wrap `new URL()` in `try/catch`.

  2. `URL.canParse()` returns a boolean for validity checks, while `URL.parse()` behaves like a constructor but returns `null` instead of throwing.

  3. Both APIs support an optional base URL, making it easier to handle relative paths, user input, and API responses.

  4. They cut repetitive helper code, avoid exceptions in normal validation flows, and make URL handling more consistent and readable.

Read the original