Bird
0
0

Identify the error in this Node.js code snippet:

medium📝 Debug Q14 of 15
Node.js - URL and Query String Handling
Identify the error in this Node.js code snippet:
const url = 'https://example.com/search?query=Node.js';
const encodedUrl = encodeURIComponent(url);
console.log(encodedUrl);
ANo error; code works correctly.
BShould use encodeURI instead of encodeURIComponent for full URLs.
CMissing decodeURIComponent call after encoding.
DencodeURIComponent cannot encode dots ('.').
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between encodeURI and encodeURIComponent

    encodeURIComponent encodes all special characters, including ':' and '/', which breaks full URLs.
  2. Step 2: Identify correct function for full URLs

    encodeURI is designed to encode full URLs without breaking their structure.
  3. Final Answer:

    Should use encodeURI instead of encodeURIComponent for full URLs. -> Option B
  4. Quick Check:

    Full URL encoding = encodeURI() [OK]
Quick Trick: Use encodeURI for full URLs, encodeURIComponent for parts [OK]
Common Mistakes:
  • Using encodeURIComponent on full URLs
  • Expecting decode after encoding always
  • Thinking dots cannot be encoded

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes