Bird
0
0

What will be the output of this Node.js code?

medium📝 component behavior Q13 of 15
Node.js - URL and Query String Handling
What will be the output of this Node.js code?
const url = new URL('https://example.com:8080/path/page?query=123#section');
console.log(url.hostname);
console.log(url.port);
console.log(url.pathname);
console.log(url.hash);
Aexample.com /path/page section
Bhttps://example.com 8080 path/page section
Cexample.com:8080 /path/page #section
Dexample.com 8080 /path/page #section
Step-by-Step Solution
Solution:
  1. Step 1: Understand URL properties

    hostname gives domain without port, port gives port number, pathname gives path starting with '/', hash includes '#' plus fragment.
  2. Step 2: Match values from the URL

    Hostname is 'example.com', port is '8080', pathname is '/path/page', hash is '#section'.
  3. Final Answer:

    example.com 8080 /path/page #section -> Option D
  4. Quick Check:

    URL parts match output A [OK]
Quick Trick: hostname excludes port; hash includes '#' [OK]
Common Mistakes:
  • Including port in hostname
  • Missing leading slash in pathname
  • Omitting '#' in hash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes