Creating and parsing query strings In a moment of boredom, I decided to write two methods, one of which would create a query string, while the other would parse it, the constraint I imposed on myself being that each method could not exceed one statement. The result is by no means something I'd ever consider using at work or in my personal projects, for that matter; my reason for posting this question is that I'd like to know how, given the constraint I set for myself, the code could be improved on? Like, what's the best code you could write to solve the problem at hand - creating and parsing a query string - without having the two methods constitute more than 2 statements? I'm sure you could do it in a much more elegant manner, and I'm looking forward to any feedback coming my way!

const createQueryString = input =&gt; encodeURI(`?${Object.keys(input).map(name 
=&gt; `${name}=${input[name]}`).join('&amp;')}`);

const queryString = createQueryString({
  name: 'Darrell',
  age: '3     2',
  nationality: 'Swedish'
});

console.log(queryString);

const parseQueryString = (input, obj = Object.create(null)) =&gt; 
decodeURI(input.slice(input.indexOf('?') + 1, 
input.length)).split('&amp;').map(entry =&gt; [
  [entry.split('=')[0]],
  [entry.split('=')[1]]
]).reduce((obj, query) =&gt; Object.assign(obj, {
  [query[0][0]]: query[1][0]
}), {});

const parsedQueryString = parseQueryString(queryString);

console.log(parsedQueryString);
Well this is a fascinating question!

I don't see much you could do in createQueryString besides potentially using Object.entries instead of Object.keys and a slight rearrangement to get rid of the outer template string.

const createQueryString = input =&gt; 
  '?' + encodeURI(Object.entries(input)
    .map(([name, value]) =&gt; `${name}=${value}`).join('&amp;'));


Do note that there is a problem here if your name or value contains &amp;. To fix this you would need to call encodeURIComponent on each part and then drop the outer enocdeURI call. 

const createQueryString = input =&gt; 
  '?' + Object.entries(input)
    .map(([name, value]) =&gt; 
      `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
    ).join('&amp;');


parseQueryString can be improved in several ways. 


You don't use obj = Object.create(null), so get rid of it. 
There is a (mostly) a built in way to do this which I will pretend doesn't exist for the rest of this post. (One disadvantage with this method is that it requires a full URI, not just the query string)

[...new URL(uri).searchParams.entries()]
    .reduce((obj, entry) =&gt; Object.assign(obj, { [entry[0]]: entry[1] }), {})

You can use regular expressions to simplify the code slightly. 

const parseQueryString = (input) =&gt; 
  input.slice(input.indexOf('?') + 1)
  .match(/[\w\d%\-!.~'()\*]+=[\w\d%\-!.~'()\*]+/g)
  .map(s =&gt; s.split('=').map(decodeURIComponent))
  .reduce((obj, [key, value]) =&gt; Object.assign(obj, { [key]: value }), {})



Full demo:



const createQueryString = input =&gt; 
  '?' + Object.entries(input)
    .map(([name, value]) =&gt; 
      `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
    ).join('&amp;');

const queryString = createQueryString({
  name: 'Darrell',
  age: '3     2',
  nationality: 'Swedish',
  messy: '&amp;hi=bye'
});

const parseQueryString = input =&gt; 
  input.slice(input.indexOf('?') + 1)
  .match(/[\w\d%\-!.~'()\*]+=[\w\d%\-!.~'()\*]+/g)
  .map(s =&gt; s.split('=').map(decodeURIComponent))
  .reduce((obj, [key, value]) =&gt; Object.assign(obj, { [key]: value }), {})

const parsedQueryString = parseQueryString(queryString);

console.log(parsedQueryString)