Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
Params without value in URLSearchParams?

I'd like to get an a=1&b=2&foo URL with URLSearchParams.

Here's what I tried:

new URLSearchParams([['a', 1], ['b', 2], ['foo']]).toString();
// Uncaught TypeError: Failed to construct 'URLSearchParams':
// Failed to construct 'URLSearchParams': Sequence initializer must only contain pair elements

new URLSearchParams([['a', 1], ['b', 2], ['foo', '']]).toString();
// a=1&b=2&foo=

new URLSearchParams([['a', 1], ['b', 2], ['foo', null]]).toString();
// a=1&b=2&foo=null

new URLSearchParams([['a', 1], ['b', 2], ['foo', undefined]]).toString();
// a=1&b=2&foo=undefined

Can it be done?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It doesn't look possible. The URL standard says that the query object that a URLSearchParams has internally is a list of name-value pairs, and that when stringified, it runs the application/x-www-form-urlencoded serializer, which does:

Set encoding to the result of getting an output encoding from encoding.
Let output be the empty string.
For each tuple of tuples:
  Let name be the result of running percent-encode after encoding with encoding, tuple’s name, the application/x-www-form-urlencoded percent-encode set, and true.
  Let value be the result of running percent-encode after encoding with encoding, tuple’s value, the application/x-www-form-urlencoded percent-encode set, and true.
  If output is not the empty string, then append U+0026 (&) to output.
  Append name, followed by U+003D (=), followed by value, to output.
Return output.

Due to that final line in the loop, a = is always appended regardless of value.

about 4 years ago · Juan Pablo Isaza Report

0

You can call toString and use a regex to remove the = after parameters with no value.

const params = new URLSearchParams([['a', 1], ['b', 2], ['foo', ''], ['bar', '']])
                .toString().replace(/=(?=&|$)/gm, '');

console.log(params)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!