Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace querystring.stringify with URLSearchParams?

querystring.stringify({
    error: 'error_status'
})

querystring is deprecated, how would I replace it with the native URLSearchParams here?

like image 645
benjosua Avatar asked Oct 21 '25 11:10

benjosua


1 Answers

const params = new URLSearchParams({
  error: 'error_status'
});

console.log(params.toString());
// error=error_status

console.log(`?${params.toString()}`);
// ?error=error_status

More information


Here's another example demonstrating how array values are handled:

let arrayParams = new URLSearchParams({
    category: ['cat', 'dog']
});

arrayParams.append('tag', 'cat');
arrayParams.append('tag', 'dog');

console.log(arrayParams.toString());
// category=cat%2Cdog&tag=cat&tag=dog

console.log(decodeURIComponent('cat%2Cdog'))
// cat,dog
like image 58
Ross Avatar answered Oct 23 '25 00:10

Ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!