Bird
0
0

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:
  1. Step 1: Check URL constructor usage

    The URL constructor requires a full URL string including protocol, e.g., 'https://example.com'.
  2. 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.
  3. Final Answer:

    const url = new URL('https://example.com'); -> Option A
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes