JS Built-ins
← URL15 / 20
URLadvanced

URL#toJSON() β€” used by JSON.stringify()

πŸ“– Usage Example
new URL('https://a.com').href  // β†’ 'https://a.com/'

href returns the full normalized URL string including trailing slash.

URL#toJSON() β€” JSON serialization

JSON.stringify() calls toJSON() on objects that have it. This means a URL inside an object serializes to its string form automatically.

Challenge: Verify that JSON.stringify({ endpoint: url }) serializes the URL correctly.

πŸ’‘ Hints (1)
  • β€’ JSON.stringify automatically calls toJSON() β€” URL serializes as its href string