Sometimes, we want to delete a query string parameter in JavaScript.
In this article, we’ll look at how to delete a query string parameter in JavaScript.
How to delete a query string parameter in JavaScript?
To delete a query string parameter in JavaScript, we can use the URLSearchParams
constructor.
For instance, we write
const params = new URLSearchParams("param1=1¶m2=2¶m3=3");
console.log(params.toString());
params.delete("param2");
console.log(params.toString());
to pass in the query string into the URLSearchParams
constructor.
Then we call delete
on the params
object to remove the query parameter with key 'param2'
.
Then we convert the params
object to a string with toString
.
Conclusion
To delete a query string parameter in JavaScript, we can use the URLSearchParams
constructor.