How to properly encode a URL in JavaScript? Difference between encodeURI, encodeURIComponent and escape
Hi everyone! I'm new to JavaScript and ran into a problem when working with URLs. I need to send a GET request to a server with parameters that contain special characters, such as spaces, Cyrillic letters, or question marks.\n\nHere's an example of my code:\n\n```javascript\nlet baseUrl = "https://api.example.com/search";\nlet query = "JavaScript & HTML/CSS уроки";\nlet fullUrl = baseUrl + "?q=" + query;\n\nfetch(fullUrl)\n .then(response => response.json())\n .then(data => console.log(data));\n```\n\nThis code doesn't work because the URL contains spaces and the `&` character. I googled it and found three functions: `escape()`, `encodeURI()`, and `encodeURIComponent()`. But I'm confused — which one is correct? When I use `escape()`, it seems to work, but I've heard it's deprecated. And `encodeURI()` doesn't encode all