1var set1 = ";,/?:@&=+$"; // Reserved Characters
2var set2 = "-_.!~*'()"; // Unescaped Characters
3var set3 = "#"; // Number Sign
4var set4 = "ABC abc 123"; // Alphanumeric Characters + Space
5
6console.log(encodeURI(set1)); // ;,/?:@&=+$
7console.log(encodeURI(set2)); // -_.!~*'()
8console.log(encodeURI(set3)); // #
9console.log(encodeURI(set4)); // ABC%20abc%20123 (the space gets encoded as %20)
10
11console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24
12console.log(encodeURIComponent(set2)); // -_.!~*'()
13console.log(encodeURIComponent(set3)); // %23
14console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)
15
16
1The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character.
2console.log(encodeURIComponent('?x=test'));
3// expected output: "%3Fx%3Dtest"