In this article, I will explain cover how you can convert a query string into an object and vice versa, which will allow you to easily get and set URL query parameters.
Despite this being a common task, I find myself googling it repeatedly because there isn't only one way of doing it.
But before we start, what is a query string again?
A query string is the part of the URL that comes after the question mark.
When you google something, for instance, you can see your search query in the URL as a query parameter:
When working with URLs, parsing and creating these queries is a common task, which used to require a manual function that parses the string.
Nowadays, however, this is much easier thanks to the URLSearchParams interface, which we'll mainly use in this tutorial.
The browser support of this API is quite good as well. As you can see here, more than 95% of all users are covered.