Default value for base in new URL

In the URL constructor documentation, it’s written that the parameter base has '' as default value. But using '' for base, I get the following error (same with Chromium or Node.js):

new URL('http://foo.com', '')
// Uncaught TypeError: URL constructor:  is not a valid URL.

With undefined or without base, the behavior is the same. The real default value is undefined?

Yes, I think you are right.

If you do new URL('foo', 'https://developer.mozilla.org'), then it works, and you end up with an href value of https://developer.mozilla.org/foo

If you do new URL('foo') or new URL('foo', undefined), then it fails because foo is not a value URL on its own.

If you do new URL('https://developer.mozilla.org') or new URL('https://developer.mozilla.org', undefined), then it works because https://developer.mozilla.org is a valid URL on its own.

I’ll update the reference page as suggested.

1 Like