Lädt...


🔧 Why should you use URL Constructor instead of template literals


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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.

...

🔧 Why should you use URL Constructor instead of template literals


📈 87.53 Punkte
🔧 Programmierung

🔧 How to validate constructor arguments when using constructor property promotion


📈 36.9 Punkte
🔧 Programmierung

🔧 Tech notes 01 - Default Constructor and User-Defined Constructor in Java


📈 36.9 Punkte
🔧 Programmierung

🔧 How to Use Template Literals in JavaScript


📈 36.37 Punkte
🔧 Programmierung

🔧 Magic of Template Literals in JavaScript


📈 32.52 Punkte
🔧 Programmierung

🔧 HTML and Templates & JavaScript Template Literals


📈 32.52 Punkte
🔧 Programmierung

🔧 HTML and Templates & JavaScript Template Literals


📈 32.52 Punkte
🔧 Programmierung

🎥 Using template literals to format strings [13 of 51] | Beginner's Series to JavaScript


📈 32.52 Punkte
🎥 Video | Youtube

🎥 Demo: Using template literals to format strings [14 of 51] | Beginner's Series to JavaScript


📈 32.52 Punkte
🎥 Video | Youtube

🔧 TIL: Tag Function / Tagged Template Literals


📈 32.52 Punkte
🔧 Programmierung

🔧 Mastering Template Literals in JavaScript


📈 32.52 Punkte
🔧 Programmierung

🔧 Introduction to ES6+ Features: Template Literals, Spread Operator, and Destructuring


📈 32.52 Punkte
🔧 Programmierung

🔧 Template Literals in TypeScript 📚✍️


📈 32.52 Punkte
🔧 Programmierung

🔧 Understanding Tagged Template Literals in JavaScript


📈 32.52 Punkte
🔧 Programmierung

🔧 JavaScript: String Template Literals


📈 32.52 Punkte
🔧 Programmierung

🔧 Why You Should Avoid `var` and Use `let` and `const` Instead


📈 28.92 Punkte
🔧 Programmierung

🔧 Why you should ditch VSCode, and use Neovim instead.


📈 28.92 Punkte
🔧 Programmierung

📰 Crypto Tips For Beginners: Why You Should Use An Exchange Instead Of A Wallet


📈 28.92 Punkte
📰 IT Security Nachrichten

🔧 TS1085: Octal literals are not available when targeting ECMAScript 5 and higher. Use the syntax '{0}'


📈 26.68 Punkte
🔧 Programmierung

🐧 Why should i use another distro instead of arch?


📈 25.67 Punkte
🐧 Linux Tipps

🐧 Why should i use another distro instead of arch?


📈 25.67 Punkte
🐧 Linux Tipps

🪟 Four reasons why you should start with Bing Chat instead of ChatGPT


📈 25.07 Punkte
🪟 Windows Tipps

📰 Skip the drip: Why you should be siphon-brewing your coffee instead


📈 25.07 Punkte
📰 IT Nachrichten

🔧 Why You Should Consider Going to University for Programming (Instead of Self-Taught)


📈 25.07 Punkte
🔧 Programmierung

🔧 3 Reasons Why you should go to the university instead of learn by yourself


📈 25.07 Punkte
🔧 Programmierung

📰 Why you should stop using your solar-powered power bank (and try this alternative instead)


📈 25.07 Punkte
📰 IT Nachrichten

📰 Why Accepting ‘Any Data Job’ Is a Terrible Career Move, and What You Should Do Instead


📈 25.07 Punkte
🔧 AI Nachrichten

🔧 The GitOps Kubernetes starter template that gets you set-up in minutes instead of hours


📈 24.39 Punkte
🔧 Programmierung

🐧 YSK: You usually should use "&&" instead of ";" in Bash


📈 24.33 Punkte
🐧 Linux Tipps

📰 Should you use Buy Now, Pay Later instead of a 0% APR credit card offer?


📈 24.33 Punkte
📰 IT Nachrichten

🔧 How to Create a Good Pull Request Template (and Why You Should Add Gifs)


📈 23.32 Punkte
🔧 Programmierung

🔧 Styled-Components: Why you should (or should not) use it


📈 23.26 Punkte
🔧 Programmierung

matomo