Bird
0
0

Identify the error in this code snippet that tries to add a query parameter:

medium📝 Debug Q14 of 15
Node.js - URL and Query String Handling
Identify the error in this code snippet that tries to add a query parameter:
const url = new URL('https://example.com');
url.searchParams.add('key', 'value');
console.log(url.href);
AThe 'href' property is read-only and cannot be logged.
BURL constructor is missing the protocol.
CYou cannot modify searchParams after URL creation.
DThe method 'add' does not exist on searchParams; should use 'append'.
Step-by-Step Solution
Solution:
  1. Step 1: Check searchParams methods

    The correct method to add a query parameter is 'append', not 'add'.
  2. Step 2: Validate other code parts

    URL has protocol, searchParams can be modified, and href can be logged. Only method name is wrong.
  3. Final Answer:

    The method 'add' does not exist on searchParams; should use 'append'. -> Option D
  4. Quick Check:

    Use searchParams.append, not add [OK]
Quick Trick: Use 'append' to add query parameters, not 'add' [OK]
Common Mistakes:
  • Using 'add' instead of 'append'
  • Thinking href is not accessible
  • Believing searchParams is immutable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes