Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the URL class in Node.js?
The URL class helps you create and manipulate URLs easily by breaking them into parts like protocol, hostname, pathname, and query parameters.
Click to reveal answer
beginner
How do you add query parameters to a URL using Node.js URL class?
You use the URLSearchParams object to add or update query parameters. For example, url.searchParams.append('key', 'value') adds a new query parameter.
Click to reveal answer
beginner
What does the url.toString() method do?
It converts the URL object back into a full URL string that you can use in requests or display.
Click to reveal answer
intermediate
Why is it better to build URLs programmatically instead of string concatenation?
Building URLs programmatically avoids errors like missing slashes or wrong encoding. It ensures the URL parts are combined correctly and safely.
Click to reveal answer
beginner
Which Node.js module provides the URL class?
The URL class is available globally in Node.js since version 10, but it originally comes from the 'url' module.
Click to reveal answer
Which method adds a query parameter to a URL object in Node.js?
Aurl.setParam()
Burl.addQuery()
Curl.searchParams.append()
Durl.query.add()
✗ Incorrect
The correct method is url.searchParams.append() to add query parameters.
What does url.toString() return?
AA URL object
BA full URL string
COnly the hostname
DOnly the query parameters
✗ Incorrect
url.toString() returns the full URL as a string.
Why should you avoid building URLs by simple string concatenation?
AIt is slower
BIt is not supported in Node.js
CIt uses more memory
DIt can cause errors like missing slashes or wrong encoding
✗ Incorrect
String concatenation can cause errors like missing slashes or incorrect encoding.
Which part of the URL can you change using the URL class properties?
Aprotocol, hostname, pathname, searchParams
BOnly the protocol
COnly the hostname
DOnly the query string as a whole
✗ Incorrect
You can change protocol, hostname, pathname, and searchParams individually.
How do you create a new URL object in Node.js?
Anew URL('https://example.com')
BURL.create('https://example.com')
Curl.parse('https://example.com')
Durl.new('https://example.com')
✗ Incorrect
You create a new URL object by calling new URL('https://example.com').
Explain how to build a URL with query parameters programmatically in Node.js.
Think about creating the URL object, adding parameters, then converting to string.
You got /3 concepts.
Why is using the URL class safer than manual string concatenation when building URLs?
Consider common mistakes when typing URLs by hand.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using the URL class in Node.js when building URLs programmatically?
easy
A. It automatically encodes special characters and manages URL parts safely.
B. It allows direct string concatenation without any checks.
C. It disables query parameters to simplify URLs.
D. It only works with HTTP URLs, ignoring others.
Solution
Step 1: Understand the purpose of the URL class
The URL class helps build URLs by handling encoding and structure automatically.
Step 2: Compare options with URL class features
Only It automatically encodes special characters and manages URL parts safely. correctly describes automatic encoding and safe URL part management.
Final Answer:
It automatically encodes special characters and manages URL parts safely. -> Option A
Quick Check:
URL class = safe URL building [OK]
Hint: Remember: URL class handles encoding and structure safely [OK]
Common Mistakes:
Thinking URL class just concatenates strings
Assuming URL class disables query parameters
Believing URL class only supports HTTP URLs
2. Which of the following is the correct way to create a new URL object for 'https://example.com' in Node.js?
easy
A. const url = new URL('https://example.com');
B. const url = new URL('example.com');
C. const url = URL('https://example.com');
D. const url = new URL(); url.href = 'https://example.com';
Solution
Step 1: Check URL constructor usage
The URL constructor requires a full URL string including protocol, e.g., 'https://example.com'.
Step 2: Validate each option
const url = new URL('https://example.com'); correctly uses new URL with full URL string. const url = new URL('example.com'); misses protocol, C misses 'new', D uses empty constructor which is invalid.
Final Answer:
const url = new URL('https://example.com'); -> Option A
Quick Check:
Use new URL('full-url') syntax [OK]
Hint: Always include protocol and use 'new' with URL [OK]
Common Mistakes:
Omitting 'new' keyword
Passing URL without protocol
Trying to create URL without constructor
3. What will be the output of this Node.js code?
const url = new URL('https://example.com/path');
url.searchParams.append('q', 'nodejs');
url.searchParams.append('page', '2');
console.log(url.toString());
medium
A. https://example.com/path?page=2&q=nodejs
B. https://example.com/path?q=nodejs&page=2
C. https://example.com/path?q=nodejs&page=2&
D. https://example.com/path
Solution
Step 1: Understand searchParams.append behavior
Appending 'q=nodejs' then 'page=2' adds these query parameters in order.
Step 2: Check URL string output
Calling toString() returns full URL with query string '?q=nodejs&page=2' appended to path.
Final Answer:
https://example.com/path?q=nodejs&page=2 -> Option B
Quick Check:
Appended params appear in order in URL string [OK]
Hint: searchParams.append adds parameters in order [OK]
Common Mistakes:
Assuming parameters order is reversed
Expecting trailing '&' at end
Ignoring appended query parameters
4. 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);
medium
A. The 'href' property is read-only and cannot be logged.
B. URL constructor is missing the protocol.
C. You cannot modify searchParams after URL creation.
D. The method 'add' does not exist on searchParams; should use 'append'.
Solution
Step 1: Check searchParams methods
The correct method to add a query parameter is 'append', not 'add'.
Step 2: Validate other code parts
URL has protocol, searchParams can be modified, and href can be logged. Only method name is wrong.
Final Answer:
The method 'add' does not exist on searchParams; should use 'append'. -> Option D
Quick Check:
Use searchParams.append, not add [OK]
Hint: 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
5. You want to build a URL with base 'https://api.example.com/search' and add parameters 'query' with value 'node.js tips' and 'sort' with value 'recent'. Which code correctly builds this URL with proper encoding?
hard
A. const url = new URL('https://api.example.com/search');
url.searchParams.set('query', 'node.js tips');
url.searchParams.set('sort', 'recent');
console.log(url.toString());
B. const url = 'https://api.example.com/search?query=node.js tips&sort=recent';
console.log(url);
C. const url = new URL('https://api.example.com/search');
url.searchParams.append('query', 'node.js tips');
url.searchParams.append('sort', 'recent');
console.log(url.href);
D. const url = new URL('https://api.example.com/search');
url.searchParams.add('query', 'node.js tips');
url.searchParams.add('sort', 'recent');
console.log(url.href);
Solution
Step 1: Understand URL and searchParams usage
Use new URL with base URL, then append parameters with searchParams.append to add multiple values safely.
Step 2: Check encoding and method correctness
const url = new URL('https://api.example.com/search');
url.searchParams.append('query', 'node.js tips');
url.searchParams.append('sort', 'recent');
console.log(url.href); uses append correctly and prints href, which includes proper encoding of spaces as '%20'. const url = new URL('https://api.example.com/search');
url.searchParams.set('query', 'node.js tips');
url.searchParams.set('sort', 'recent');
console.log(url.toString()); uses set which replaces values but also works; however, question asks for correct code with proper encoding and append is more common for adding multiple params. const url = 'https://api.example.com/search?query=node.js tips&sort=recent';
console.log(url); is manual string and misses encoding. const url = new URL('https://api.example.com/search');
url.searchParams.add('query', 'node.js tips');
url.searchParams.add('sort', 'recent');
console.log(url.href); uses non-existent 'add' method.
Final Answer:
Option C code correctly builds URL with proper encoding and appends parameters. -> Option C
Quick Check:
Use URL + searchParams.append for safe, encoded URLs [OK]
Hint: Use URL and searchParams.append for safe, encoded query parameters [OK]
Common Mistakes:
Manually concatenating query strings without encoding
Using non-existent 'add' method
Ignoring encoding of spaces and special characters