Checking a value before assigning it to variable Throughout my code, I find myself testing if the output of a function is undefined or blank before i use the output. In this case, getQuery checks if the property of an object is either blank or if the object key is not set at all. In both cases, the output can be ignored. But if the object key holds a value, then I want to use this output as the value for my variable.

var filter = ""
if ( typeof getQuery("filter") != 'undefined' &amp;&amp; getQuery("filter") != "" ) filter = getQuery("filter")
if ( filter != "" ) createFilterView( filter ) 


There must be a more optimized way of doing this without the repetition?

I often set the output of the function to a variable first, in order to not have to perform the function several times. A bit faster, I assume, but also not very elegant.

var getQueryFilter_output = getQuery("filter")
if ( getQueryFilter_output != 'undefined' &amp;&amp; getQueryFilter_output != "" ) var filter = getQuery_output 


What would be a better way of doing this?
Indentation

I'm not quite sure if this is correct, but from what I know, I believe it is.

Indentation in JavaScript does not matter as, before your code is run, it is completely minimized - meaning it removes all comments and unnecessary spaces.

That being said, trying to crush your JavaScript into three lines does not make it any faster.

Calling

In your first example, you call the same function three times with the same arguments each time. This is extremely inefficient.

It would be optimal to call it once and save it in a variable.

Therefore, these lines:

var filter = ""
if ( typeof getQuery("filter") != 'undefined' &amp;&amp; getQuery("filter") != "" ) filter = getQuery("filter")
if ( filter != "" ) createFilterView( filter ) 


Can become:

var filter = "";
var ret = getQuery("filter"); // for lack of a better variable name
if(typeof ret != 'undefined' &amp;&amp; ret != "") filter = ret;
if(filter != "") createFilterView(filter)


(Yes, you fixed this in the second example)

Conditionals 1

In your first example, you check to see that the return value of getQuery isn't "undefined" and that it isn't "". If that passed, you take the return value and place it in a variable. Then, you go through another conditional to make sure that that variable isn't empty.

Well, since in your first conditional you already check to make sure that the return value isn't empty, why don't you put the body of the second conditional into the first one.

This:

if ( typeof getQuery("filter") != 'undefined' &amp;&amp; getQuery("filter") != "" ) filter = getQuery("filter")
if ( filter != "" ) createFilterView( filter ) 


Becomes:

if(typeof getQuery("filter") != 'undefined' &amp;&amp; getQuery("filter") != "") {
    filter = getQuery("filter")
    createFilterView(filter)
}


Conditionals 2

In the conditional statements in both of your examples, you check that the return value of getQuery is not "". You don't have to specifically specify that.

You can just remove the != "" part and it would work the same way.

The further reduce the size of your conditional, you can have your getQuery function actually return the value undefined (null would be better, however) instead of it returning a string of the word "undefined".

Then, you could reduce your conditional to:

if(getQuery("filter"))


Semicolons

In JavaScript, it is a good practice to close statements with a semicolon.

Example:

createFilterView(filter)


Should be:

createFilterView(filter);
                        ^
                        |
                    semicolon


Quotes

This isn't really important, but I just thought I'd point it out.

You are consistent in your code with your quotes. For some strings, you use single quotes and for others, you use double quotes.

It is best to stick to one.