Bird
0
0

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:
  1. Step 1: Recall URL object creation syntax

    In Node.js, the URL class is used with the new keyword: new URL(string).
  2. 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.
  3. Final Answer:

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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes