Complete the code to encode a URL component using Node.js built-in function.
const encoded = encodeURIComponent([1]);The encodeURIComponent function encodes a string representing a URL component. You need to pass the string variable to encode.
Complete the code to decode an encoded URL component string.
const decoded = [1](encoded);The decodeURIComponent function reverses the encoding done by encodeURIComponent. It decodes percent-encoded characters.
Fix the error in this code that tries to encode a full URL string.
const safeUrl = [1](url);To encode a full URL, use encodeURI. encodeURIComponent is for parts of URLs and encodes characters like slashes which breaks full URLs.
Fill both blanks to encode a query parameter key and value safely.
const query = [1](key) + '=' + [2](value);
Both the key and value of a query parameter should be encoded with encodeURIComponent to ensure special characters are safely encoded.
Fill all three blanks to decode a full URL, extract a query string, and decode a parameter value.
const decodedUrl = [1](url); const queryString = decodedUrl.split('?')[[2]]; const paramValue = [3](queryString.split('=')[1]);
First, decode the full URL with decodeURI. Then get the part after '?' which is index 1. Finally, decode the parameter value with decodeURIComponent.