How to Decode a Query String in JavaScript?

Estimated read time 1 min read

In JavaScript, you can parse a query string into an object using the new URLSearchParams() constructor or by splitting and parsing the string manually.

Here’s an example using URLSearchParams:

const queryString = '?name=John&age=30';
const params = new URLSearchParams(queryString);
const name = params.get('name');
const age = params.get('age');
console.log(name, age); // "John" 30

Here’s an example of splitting and parsing the string manually:

const queryString = '?name=John&age=30';
const queries = queryString.substring(1).split('&');
const params = {};
for (const query of queries) {
  const [key, value] = query.split('=');
  params[key] = value;
const name =;
const age = params.age;
console.log(name, age); // "John" 30

In both examples, the query string '?name=John&age=30' is parsed into an object with properties name and age. The values of name and age are then extracted and logged to the console.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply