Bird
0
0

What will be the output of this Node.js code snippet? const myURL = new URL('https://example.com:8080/path?search=books#top'); console.log(myURL.port); console.log(myURL.hash);

medium📝 Predict Output Q4 of 15
Node.js - URL and Query String Handling
What will be the output of this Node.js code snippet? const myURL = new URL('https://example.com:8080/path?search=books#top'); console.log(myURL.port); console.log(myURL.hash);
A8080 #top
B8080 top
Cundefined #top
Dundefined top
Step-by-Step Solution
Solution:
  1. Step 1: Extract port from URL

    The URL has port 8080, so myURL.port returns '8080' as a string.
  2. Step 2: Extract hash from URL

    The hash includes the '#' symbol, so myURL.hash returns '#top'.
  3. Final Answer:

    8080 #top -> Option A
  4. Quick Check:

    URL port and hash include port number and '#' [OK]
Quick Trick: URL.port returns string port; URL.hash includes '#' [OK]
Common Mistakes:
  • Expecting port as number instead of string
  • Missing '#' in hash output
  • Assuming undefined for port when present

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes