Bird
0
0

You want to change the query parameter id to 42 in this URL: https://shop.com/products?category=books&id=10. Which code correctly updates the URL using the URL class?

hard📝 Application Q15 of 15
Node.js - URL and Query String Handling
You want to change the query parameter id to 42 in this URL: https://shop.com/products?category=books&id=10. Which code correctly updates the URL using the URL class?
Aconst url = new URL('https://shop.com/products?category=books&id=10'); url.searchParams.set('id', '42'); console.log(url.href);
Bconst url = new URL('https://shop.com/products?category=books&id=10'); url.query.id = 42; console.log(url.href);
Cconst url = new URL('https://shop.com/products?category=books&id=10'); url.search.id = '42'; console.log(url.href);
Dconst url = new URL('https://shop.com/products?category=books&id=10'); url.setQuery('id', '42'); console.log(url.href);
Step-by-Step Solution
Solution:
  1. Step 1: Identify how to update query parameters

    The URL class provides searchParams with methods like set() to update query parameters safely.
  2. Step 2: Check each option's method

    const url = new URL('https://shop.com/products?category=books&id=10'); url.searchParams.set('id', '42'); console.log(url.href); uses searchParams.set(), which is correct. Options A, C, and D use invalid properties or methods not available on URL objects.
  3. Final Answer:

    const url = new URL('https://shop.com/products?category=books&id=10'); url.searchParams.set('id', '42'); console.log(url.href); -> Option A
  4. Quick Check:

    Use searchParams.set() to update query [OK]
Quick Trick: Use url.searchParams.set() to change query values [OK]
Common Mistakes:
  • Trying to set query directly as object
  • Using non-existent methods like setQuery
  • Assigning query parameters without searchParams

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes