How to convert a query string to an object in JavaScript

1 Answer

0 votes
const queryString = "?q=JavaScript&lang=en&limit=10";

let obj = new URLSearchParams(queryString);

console.log(obj.get('q')); 
console.log(obj.get('lang'));
console.log(obj.get('limit'));

obj = Object.fromEntries(new URLSearchParams(queryString));

console.log(obj);

  
  
  
  
/*
run:
  
"JavaScript"
"en"
"10"
{
  lang: "en",
  limit: "10",
  q: "JavaScript"
}

*/
  

 



answered Apr 22, 2022 by avibootz
edited Apr 22, 2022 by avibootz

Related questions

3 answers 325 views
3 answers 420 views
1 answer 247 views
1 answer 214 views
3 answers 316 views
3 answers 422 views
...