Bird
0
0

Which of the following is the correct way to create a new URL object in Node.js?

easy📝 Syntax Q3 of 15
Node.js - URL and Query String Handling
Which of the following is the correct way to create a new URL object in Node.js?
Aconst myUrl = URL('https://example.com/path?query=1');
Bconst myUrl = new URL('https://example.com/path?query=1');
Cconst myUrl = url.parse('https://example.com/path?query=1');
Dconst myUrl = new url('https://example.com/path?query=1');
Step-by-Step Solution
Solution:
  1. Step 1: Recall URL object creation syntax

    The URL class requires the new keyword and a string argument.
  2. Step 2: Identify incorrect usages

    Calling URL without new or using url.parse (which is from the legacy 'url' module) is not the modern way.
  3. Final Answer:

    const myUrl = new URL('https://example.com/path?query=1'); -> Option B
  4. Quick Check:

    URL object creation = new URL(string) [OK]
Quick Trick: Use 'new URL()' to create URL objects [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using deprecated url.parse instead
  • Calling URL as a function without 'new'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes