Bird
0
0

Given the code below, what will console.log(myUrl.searchParams.get('id')) output?

medium📝 component behavior Q5 of 15
Node.js - URL and Query String Handling
Given the code below, what will console.log(myUrl.searchParams.get('id')) output?
const myUrl = new URL('https://site.com/page?name=bob&id=123&ref=abc');
Aabc
Bbob
C123
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Locate the 'id' query parameter in the URL

    The URL query string contains id=123.
  2. Step 2: Use searchParams.get to retrieve the value

    searchParams.get('id') returns the value of the 'id' parameter as a string.
  3. Final Answer:

    123 -> Option C
  4. Quick Check:

    searchParams.get('id') = '123' [OK]
Quick Trick: searchParams.get('key') returns query value string [OK]
Common Mistakes:
  • Confusing parameter names
  • Expecting number type instead of string
  • Getting null if parameter name is misspelled

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes