Challenge - 5 Problems
URLSearchParams Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this code using URLSearchParams?
Consider the following Node.js code snippet that uses
URLSearchParams. What will be printed to the console?Node.js
const params = new URLSearchParams('name=alice&age=30&city=NY'); console.log(params.get('age'));
Attempts:
2 left
💡 Hint
Remember that
get returns the value of the given key from the query string.✗ Incorrect
The get method returns the value associated with the given key. Here, age is "30".
📝 Syntax
intermediate2:00remaining
Which option correctly creates a URLSearchParams object from an object?
You want to create a
URLSearchParams instance from this object: { foo: 'bar', count: 5 }. Which code snippet works without error?Attempts:
2 left
💡 Hint
URLSearchParams accepts a string or an iterable of key-value pairs, not a plain object.
✗ Incorrect
Only option A uses an iterable of key-value pairs (array of arrays), which is valid. Options B and D pass plain objects, which cause a TypeError. Option A uses a string, which works but is not from an object.
🔧 Debug
advanced2:00remaining
Why does this code throw a TypeError?
Examine the code below. Why does it throw a TypeError?
Node.js
const params = new URLSearchParams({ key: 'value' });
console.log(params.toString());Attempts:
2 left
💡 Hint
Check the allowed argument types for URLSearchParams constructor.
✗ Incorrect
The constructor expects a string or iterable of pairs. Passing a plain object causes a TypeError.
❓ state_output
advanced2:00remaining
What is the final query string after these operations?
Given the following code, what will
params.toString() output?Node.js
const params = new URLSearchParams('a=1&b=2&c=3'); params.set('b', '20'); params.append('d', '4'); params.delete('a');
Attempts:
2 left
💡 Hint
Remember that
set replaces the value, append adds a new value, and delete removes the key.✗ Incorrect
After deleting 'a', setting 'b' to '20', and appending 'd=4', the query string is 'b=20&c=3&d=4'.
🧠 Conceptual
expert3:00remaining
Which option correctly explains how URLSearchParams handles multiple values for the same key?
If a query string contains multiple values for the same key, how does
URLSearchParams behave when using get and getAll methods?Attempts:
2 left
💡 Hint
Think about how to retrieve one or all values for a repeated key.
✗ Incorrect
get returns the first value for a key. getAll returns an array of all values for that key.