Bird
0
0

Which of the following is the correct way to create a URLSearchParams object from a query string in Node.js?

easy📝 Syntax Q12 of 15
Node.js - URL and Query String Handling
Which of the following is the correct way to create a URLSearchParams object from a query string in Node.js?
Aconst params = new URLSearchParams({name: 'John', age: 30});
Bconst params = URLSearchParams('?name=John&age=30');
Cconst params = URLSearchParams({name: 'John', age: 30});
Dconst params = new URLSearchParams('?name=John&age=30');
Step-by-Step Solution
Solution:
  1. Step 1: Check the correct syntax for creating URLSearchParams

    The constructor requires the new keyword and accepts a query string like '?name=John&age=30'.
  2. Step 2: Identify incorrect options

    Options without new or passing an object directly without conversion are invalid.
  3. Final Answer:

    const params = new URLSearchParams('?name=John&age=30'); -> Option D
  4. Quick Check:

    Use new with query string [OK]
Quick Trick: Always use 'new' with URLSearchParams and a query string [OK]
Common Mistakes:
  • Omitting the 'new' keyword
  • Passing an object directly without converting to string
  • Using URLSearchParams as a function, not a constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes