Bird
0
0

What is wrong with this code? const { URL } = require('url'); const myURL = URL('https://example.com'); console.log(myURL.pathname);

medium📝 Debug Q7 of 15
Node.js - URL and Query String Handling
What is wrong with this code? const { URL } = require('url'); const myURL = URL('https://example.com'); console.log(myURL.pathname);
Arequire('url') does not export URL class.
BURL must be called with 'new' keyword to create an instance.
Cpathname property is not available on URL instances.
DThe URL string is missing a protocol.
Step-by-Step Solution
Solution:
  1. Step 1: Check how URL is instantiated

    URL is a class and must be called with 'new' to create an object.
  2. Step 2: Verify pathname property

    pathname exists on URL instances, so no issue there.
  3. Final Answer:

    URL must be called with 'new' keyword to create an instance. -> Option B
  4. Quick Check:

    Always use new URL() to instantiate [OK]
Quick Trick: Always use 'new' with URL constructor [OK]
Common Mistakes:
  • Calling URL() without new keyword
  • Thinking pathname is missing
  • Assuming require('url') lacks URL export

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes