Categories
JavaScript Answers

How to delete a query string parameter in JavaScript?

Spread the love

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&param2=2&param3=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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *