Bird
Raised Fist0
Node.jsframework~20 mins

Encoding and decoding URLs in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
URL Encoding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of decoding a URL component?
Consider the following Node.js code snippet that decodes a URL component. What will be the output?
Node.js
const encoded = 'Hello%20World%21';
const decoded = decodeURIComponent(encoded);
console.log(decoded);
AHello World!
BHello%20World%21
CHello+World!
DSyntaxError
Attempts:
2 left
💡 Hint
decodeURIComponent converts percent-encoded characters back to normal characters.
Predict Output
intermediate
2:00remaining
What does encodeURI do with special characters?
What is the output of this Node.js code that uses encodeURI on a URL string?
Node.js
const url = 'https://example.com/a file.html?name=John Doe&age=30';
const encoded = encodeURI(url);
console.log(encoded);
Ahttps%3A%2F%2Fexample.com%2Fa%20file.html%3Fname%3DJohn%20Doe%26age%3D30
Bhttps://example.com/a%20file.html%3Fname%3DJohn%20Doe%26age%3D30
Chttps://example.com/a%20file.html?name=John%20Doe&age=30
Dhttps://example.com/a file.html?name=John Doe&age=30
Attempts:
2 left
💡 Hint
encodeURI encodes spaces but leaves URL special characters like ':' and '?' intact.
📝 Syntax
advanced
2:00remaining
Which option causes a SyntaxError when decoding a URL component?
Which of the following decodeURIComponent calls will cause a SyntaxError in Node.js?
AdecodeURIComponent('%E0%A4%A');
BdecodeURIComponent('%20');
CdecodeURIComponent('%3F');
DdecodeURIComponent('Hello%20World');
Attempts:
2 left
💡 Hint
Invalid percent-encoding sequences cause SyntaxError in decodeURIComponent.
component_behavior
advanced
2:00remaining
How does encodeURIComponent treat reserved URL characters?
Given the string 'name=John&age=30', what will encodeURIComponent produce?
Node.js
const input = 'name=John&age=30';
const output = encodeURIComponent(input);
console.log(output);
Aname%3DJohn%26age=30
Bname=John&age=30
Cname%3DJohn&age%3D30
Dname%3DJohn%26age%3D30
Attempts:
2 left
💡 Hint
encodeURIComponent encodes all characters except letters, digits, and a few others.
state_output
expert
2:00remaining
What is the final value of 'url' after encoding and decoding?
Consider this Node.js code. What is the final value of the variable 'url' after running all lines?
Node.js
let url = 'https://example.com/search?query=Node.js & encoding';
url = encodeURI(url);
url = decodeURIComponent(url);
Ahttps://example.com/search?query=Node.js%20&%20encoding
Bhttps://example.com/search?query=Node.js & encoding
Chttps://example.com/search?query=Node.js%20%26%20encoding
Dgnidocne & sj.edoN=yreuq?hcraes/moc.elpmaxe//:sptth
Attempts:
2 left
💡 Hint
encodeURI encodes spaces as '%20' but leaves '&' intact; decodeURIComponent reverses encoding.

Practice

(1/5)
1. What does the encodeURIComponent function do in Node.js?
easy
A. It decodes an encoded URL string back to normal text.
B. It parses a URL into its components.
C. It sends an HTTP request to a URL.
D. It converts special characters in a string into a format safe for URLs.

Solution

  1. Step 1: Understand the purpose of encoding URLs

    Encoding converts special characters like spaces or symbols into a safe format for URLs.
  2. Step 2: Identify the role of encodeURIComponent

    This function specifically encodes parts of a URL so they can be safely transmitted.
  3. Final Answer:

    It converts special characters in a string into a format safe for URLs. -> Option D
  4. Quick Check:

    Encoding = safe URL string [OK]
Hint: Encoding makes URL parts safe by replacing special characters [OK]
Common Mistakes:
  • Confusing encoding with decoding
  • Thinking it sends HTTP requests
  • Assuming it parses URLs
2. Which of the following is the correct syntax to decode a URL-encoded string in Node.js?
easy
A. decodeURIComponent('https%3A%2F%2Fexample.com')
B. decodeURI('https://example.com')
C. encodeURIComponent('https://example.com')
D. encodeURI('https%3A%2F%2Fexample.com')

Solution

  1. Step 1: Identify the function to decode URL components

    decodeURIComponent is used to decode encoded parts of a URL.
  2. Step 2: Check the syntax correctness

    The syntax decodeURIComponent('https%3A%2F%2Fexample.com') correctly decodes the encoded string.
  3. Final Answer:

    decodeURIComponent('https%3A%2F%2Fexample.com') -> Option A
  4. Quick Check:

    Decoding syntax = decodeURIComponent() [OK]
Hint: Use decodeURIComponent() to decode encoded URL parts [OK]
Common Mistakes:
  • Using encode functions to decode
  • Using decodeURI which decodes less strictly
  • Mixing encodeURI and decodeURIComponent
3. What will be the output of the following Node.js code?
const encoded = encodeURIComponent('Hello World!');
console.log(encoded);
medium
A. Hello%World%!
B. Hello+World!
C. Hello%20World%21
D. Hello World!

Solution

  1. Step 1: Understand what encodeURIComponent does to spaces and special characters

    Spaces become %20 and exclamation marks become %21 in encoding.
  2. Step 2: Apply encoding to 'Hello World!'

    The space is replaced by %20 and '!' by %21, resulting in 'Hello%20World%21'.
  3. Final Answer:

    Hello%20World%21 -> Option C
  4. Quick Check:

    Space = %20, ! = %21 [OK]
Hint: Spaces become %20, special chars like ! become %21 [OK]
Common Mistakes:
  • Expecting spaces to become '+'
  • Confusing encodeURI with encodeURIComponent
  • Not encoding special characters
4. 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);
medium
A. No error; code works correctly.
B. Should use encodeURI instead of encodeURIComponent for full URLs.
C. Missing decodeURIComponent call after encoding.
D. encodeURIComponent cannot encode dots ('.').

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]
Hint: 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
5. You want to safely encode a URL query parameter that contains an email address like user@example.com. Which code snippet correctly encodes this parameter for use in a URL?
hard
A. const param = encodeURIComponent('user@example.com');
B. const param = encodeURI('user@example.com').replace(/@/g, '+');
C. const param = encodeURIComponent('user@example.com').replace(/%40/g, '@');
D. const param = encodeURI('user@example.com');

Solution

  1. Step 1: Determine correct encoding for query parameters

    Query parameters should be encoded with encodeURIComponent to encode special characters like '@'.
  2. Step 2: Check each option's correctness

    const param = encodeURI('user@example.com'); uses encodeURI which does not encode '@'. const param = encodeURI('user@example.com').replace(/@/g, '+'); uses encodeURI and replaces '@' with '+', but '+' is interpreted as space in query strings. const param = encodeURIComponent('user@example.com').replace(/%40/g, '@'); uses encodeURIComponent but then decodes '%40' back to '@'. const param = encodeURIComponent('user@example.com');: const param = encodeURIComponent('user@example.com'); correctly encodes '@' as '%40'.
  3. Final Answer:

    const param = encodeURIComponent('user@example.com'); -> Option A
  4. Quick Check:

    Query param encoding = encodeURIComponent() [OK]
Hint: Use encodeURIComponent for query parameters including emails [OK]
Common Mistakes:
  • Using encodeURI for query parameters
  • Manually replacing encoded characters incorrectly
  • Not encoding '@' in emails