Add query string to a URL I have written this code which adds a querystring to a URL:

exports.addQueryString = function(url, queryString) {
  var isQuestionMarkPresent = url &amp;&amp; url.indexOf('?') !== -1,
    separator = '';

  if (queryString) {
    separator = isQuestionMarkPresent ? '&amp;' : '?';
    url += separator + queryString;
  }

  return url;
};


Is there any way I can write this better?

Usage:

addQueryString('http://example.com', 'q=1&amp;val=3&amp;user=me');
I think that is ok, only that i see not necesary make url.indexOf('?') if queryString is empty, because you are not going to use it.

exports.addQueryString = function(url, queryString) {   
  if (queryString) {
    var isQuestionMarkPresent = url &amp;&amp; url.indexOf('?') !== -1,
      separator = isQuestionMarkPresent ? '&amp;' : '?';
    url += separator + queryString;
  }

  return url;
};