Which of the following is the correct way to create a URL object 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 URL object in Node.js?
Aconst url = new URL('https://example.com');
Bconst url = URL('https://example.com');
Cconst url = url.parse('https://example.com');
Dconst url = new url('https://example.com');
Step-by-Step Solution
Solution:
Step 1: Recall URL object creation syntax
In Node.js, the URL class is used with the new keyword: new URL(string).
Step 2: Check each option for correct syntax
const url = new URL('https://example.com'); uses new URL('...'), which is correct. const url = URL('https://example.com'); misses new keyword. const url = url.parse('https://example.com'); uses url.parse which is from older API. const url = new url('https://example.com'); uses lowercase url which is invalid.
Final Answer:
const url = new URL('https://example.com'); -> Option A
Quick Check:
Use new URL() to create URL object [OK]
Quick Trick:Use 'new URL()' with capital U and new keyword [OK]
Common Mistakes:
Omitting 'new' keyword
Using lowercase 'url' instead of 'URL'
Using deprecated url.parse method
Master "URL and Query String Handling" in Node.js
9 interactive learning modes - each teaches the same concept differently