Why should you use URL Constructor instead of template literals

Hey everyone! Today, I’m sharing a quick tip that improved the semantics of my code significantly.

Often, whether working in frontend or backend development, we need to construct URLs with parameters, right?

I used to write the URLs of my requests li…


This content originally appeared on DEV Community and was authored by Leonardo Theodoro

Hey everyone! Today, I'm sharing a quick tip that improved the semantics of my code significantly.

Often, whether working in frontend or backend development, we need to construct URLs with parameters, right?

I used to write the URLs of my requests like this:

const url = `http://localhost:3000/endpoint/param1=${var1}&param2=${var2}&param3=${var3}`

We agree that this URL is hard to read and maintain; we always need to identify which parts are parameters, which are variables, and which are just Javascript syntax.

To address this semantic issue, I discovered the URL Constructor, which accomplishes the same task but in more efficient and elegant way.

Now, we can rewrite the same code like this:

const url = new URL('http://localhost:3000/endpoint')

url.searchParams.set('param1', var1)
url.searchParams.set('param2', var2)
url.searchParams.set('param3', var3)

The code clearly indicates what it's doing. In the first line, we create the base URL and in the subsequent lines, we add the necessary search parameters.

Done. Now, the variable url contains the same search parameters as before, but now we are using the URL class, making the code much simpler and easier to maintain.

What about you? Have you used the URL class before? Maybe for another purpose? Feel free to share your experiences with me.


This content originally appeared on DEV Community and was authored by Leonardo Theodoro


Print Share Comment Cite Upload Translate Updates
APA

Leonardo Theodoro | Sciencx (2024-07-24T13:20:54+00:00) Why should you use URL Constructor instead of template literals. Retrieved from https://www.scien.cx/2024/07/24/why-should-you-use-url-constructor-instead-of-template-literals-2/

MLA
" » Why should you use URL Constructor instead of template literals." Leonardo Theodoro | Sciencx - Wednesday July 24, 2024, https://www.scien.cx/2024/07/24/why-should-you-use-url-constructor-instead-of-template-literals-2/
HARVARD
Leonardo Theodoro | Sciencx Wednesday July 24, 2024 » Why should you use URL Constructor instead of template literals., viewed ,<https://www.scien.cx/2024/07/24/why-should-you-use-url-constructor-instead-of-template-literals-2/>
VANCOUVER
Leonardo Theodoro | Sciencx - » Why should you use URL Constructor instead of template literals. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/24/why-should-you-use-url-constructor-instead-of-template-literals-2/
CHICAGO
" » Why should you use URL Constructor instead of template literals." Leonardo Theodoro | Sciencx - Accessed . https://www.scien.cx/2024/07/24/why-should-you-use-url-constructor-instead-of-template-literals-2/
IEEE
" » Why should you use URL Constructor instead of template literals." Leonardo Theodoro | Sciencx [Online]. Available: https://www.scien.cx/2024/07/24/why-should-you-use-url-constructor-instead-of-template-literals-2/. [Accessed: ]
rf:citation
» Why should you use URL Constructor instead of template literals | Leonardo Theodoro | Sciencx | https://www.scien.cx/2024/07/24/why-should-you-use-url-constructor-instead-of-template-literals-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.