Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Node.js - URL and Query String Handling
What will be the output of this code?
const url = new URL('https://example.com');
url.pathname = '/products';
url.searchParams.set('id', '123');
console.log(url.toString());
Ahttps://example.com/products?id=123
Bhttps://example.com?id=123/products
Chttps://example.com/products#id=123
Dhttps://example.com/products?id=
Step-by-Step Solution
Solution:
  1. Step 1: Understand URL pathname and searchParams usage

    Setting pathname changes the path part; searchParams.set adds or updates query parameters.
  2. Step 2: Combine pathname and query string correctly

    The URL becomes 'https://example.com/products?id=123' as pathname is '/products' and query is 'id=123'.
  3. Final Answer:

    https://example.com/products?id=123 -> Option A
  4. Quick Check:

    URL with pathname and query = correct URL string [OK]
Quick Trick: Changing pathname and searchParams updates URL parts correctly [OK]
Common Mistakes:
  • Mixing query and fragment (#)
  • Appending query after path incorrectly
  • Leaving query value empty

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes