Which of the following is the correct way to create a new URL object for 'https://example.com' 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 new URL object for 'https://example.com' in Node.js?
Aconst url = new URL('https://example.com');
Bconst url = new URL('example.com');
Cconst url = URL('https://example.com');
Dconst url = new URL(); url.href = 'https://example.com';
Step-by-Step Solution
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]
Quick Trick:Always include protocol and use 'new' with URL [OK]
Common Mistakes:
Omitting 'new' keyword
Passing URL without protocol
Trying to create URL without constructor
Master "URL and Query String Handling" in Node.js
9 interactive learning modes - each teaches the same concept differently