There are a number of ways to solve this.
The most obvious is just to build your link piece by piece.
var a = 1;
var b = null;
var c = 5;
var str = '';
if (a) {
str += a + '&';
}
if (b) {
str += b + '&';
}
// ... and so on
A much cleaner solution I prefer is to actually put everything into an array or object, then build your string from that.
var data = {
a: 1,
b: null,
c: 5
};
var str = 'http://myarray.com/?' +
Object.keys(data)
.filter(function (key) { return data[key] })
.map(function (key) { return key + '=' + data[key] }).join('&');
Object.keys(data) will give you an array like ['a', 'b', 'c'] (the keys from the object).
filter() will go through and build a new array from any values that return a truthful value. In this case, the resulting array would be ['a', 'c'] ('b' got removed because it's value is null, which is a falsey value).
map() will then loop through each of those, and then build a new array from the returned value of each call. That would look like this: ['a=1', 'c=5'].
Finally, join('&') will join all the values in the array into one string: a=1&c=5, which we can just add to the rest of the string.
Another option would be to use ternary operators in the string (a ? a : ''), but this doesn't scale well with lots of values.