Difficulty: Easy
Correct Answer: encodeURI converts a string into a valid Uniform Resource Identifier by escaping special characters, and decodeURI reverses this process by decoding the escaped characters.
Explanation:
Introduction / Context:
When building web applications, you often need to construct or process URLs that contain spaces, non ASCII characters, or reserved symbols. JavaScript provides encodeURI and decodeURI to handle these cases safely. This question tests whether you understand their role in encoding and decoding Uniform Resource Identifiers.
Given Data / Assumptions:
- We are working with URL or URI strings in JavaScript code.
- Strings may contain characters that need to be escaped for safe use in a URL.
- JavaScript offers built in functions for encoding and decoding URIs.
- The focus is on URL handling, not on file compression or encryption.
Concept / Approach:
encodeURI scans a string and replaces certain characters with percent encoded sequences that are safe to use in a URL. For example, spaces become percent twenty. decodeURI reverses that transformation by converting encoded sequences back into their original characters. These functions are essential whenever you construct URLs from user input or concatenated strings to avoid broken links or errors. The correct answer must describe this encode decode behavior in the context of URIs.
Step-by-Step Solution:
Step 1: Recall that URLs cannot contain arbitrary characters and must follow encoding rules.
Step 2: Remember that encodeURI prepares a string for use as a URL by escaping special characters.
Step 3: Recognize that decodeURI is used to interpret encoded URLs back into human readable strings.
Step 4: Select the option that clearly explains this pair of functions as encoding and decoding tools for URIs.
Verification / Alternative check:
You can verify the behavior by running encodeURI("https://example.com/search?q=hello world") in the browser console. The space becomes an encoded sequence. Passing the result into decodeURI returns the original string with the space restored. This simple test confirms that encodeURI and decodeURI perform reversible transformations suitable for URL handling.
Why Other Options Are Wrong:
Option B is wrong because image compression uses different algorithms and APIs. Option C is incorrect since password encryption requires cryptographic functions, not encodeURI. Option D is false because date formatting and parsing are handled by Date methods or libraries, not by URI functions. Option E is unrelated because window management is done through window.open and similar functions.
Common Pitfalls:
A common pitfall is confusing encodeURI with encodeURIComponent, which has a slightly different scope and encodes more characters. Another issue is forgetting to decode values when displaying them to users, which leaves percent encoded sequences visible in the interface. Developers should understand when to apply encoding and decoding to avoid broken URLs and security issues such as injection attacks.
Final Answer:
encodeURI converts a string into a valid URI by escaping special characters for safe use in a web address, and decodeURI reverses this process by decoding the escaped characters back to their original form.
Discussion & Comments