Categories
JavaScript Answers

How to get query string parameters URL values with JavaScript?

Spread the love

Sometimes, we want to get query string parameters URL values with JavaScript.

In this article, we’ll look at how to get query string parameters URL values with JavaScript.

How to get query string parameters URL values with JavaScript?

To get query string parameters URL values with JavaScript, we can use the URLSearchParams constructor.

For instance, we write

const urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.get("action"));

to get the query string from the current page’s URL with window.location.search.

Then we call get with the key of the query parameter we want to get to return its value.

Conclusion

To get query string parameters URL values with 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 *