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 myUrl = new URL('https://example.com:8080/path?search=test#frag');
console.log(myUrl.hostname);
console.log(myUrl.port);
console.log(myUrl.pathname);
console.log(myUrl.search);
console.log(myUrl.hash);
Ahttps://example.com 8080 /path search=test frag
Bexample.com /path ?search=test #frag
Cexample.com:8080 /path ?search=test #frag undefined
Dexample.com 8080 /path ?search=test #frag
Step-by-Step Solution
Solution:
  1. Step 1: Understand URL properties

    myUrl.hostname returns 'example.com', port returns '8080', pathname returns '/path', search returns '?search=test', hash returns '#frag'.
  2. Step 2: Match output to options

    example.com 8080 /path ?search=test #frag matches all values exactly as expected. Others have missing or incorrect parts.
  3. Final Answer:

    example.com 8080 /path ?search=test #frag -> Option D
  4. Quick Check:

    URL parts match example.com 8080 /path ?search=test #frag output [OK]
Quick Trick: Remember URL properties return strings including '?' and '#' [OK]
Common Mistakes:
  • Forgetting '?' in search or '#' in hash
  • Confusing hostname with full URL
  • Missing port or printing undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes