URL オブジェクトは文字列コンテキストにおいて href プロパティの値になる
Published Mar 5, 2025
⋅
Updated Oct 28, 2025
URL オブジェクトを作成するには次のように URL() constructor を使う。
new URL('https://example.com'); オブジェクトの中身は次のようになる。
{
href: 'https://example.com/',
origin: 'https://example.com',
protocol: 'https:',
username: '',
password: '',
host: 'example.com',
hostname: 'example.com',
port: '',
pathname: '/',
search: '',
searchParams: URLSearchParams {},
}次のように文字列コンテキスト上で URL オブジェクトを参照すると "https://example.com/" が返ってくる。これは URL オブジェクトの href プロパティの値に相当する。
`${new URL('https://example.com')}`
// "https://example.com/"文字列コンテキストにおいては URL.toString()メソッドが使われるため、 URL.href が返される。
参考