Domanda

I would like to write a ternary if statement in javascript where you call a function and if it returns a value other then 0 set the variable to that value otherwise set it to something else ex:

selectedStartDate = (GetURLParameter("StartDate")) ? 
                     GetURLParameter("StartDate") : 
                     $("#start_date").val();

Is there a way to get rid of the double function call?

È stato utile?

Soluzione

selectedStartDate = GetURLParameter("StartDate") || $("#start_date").val();

Altri suggerimenti

selectedStartDate = GetURLParameter("StartDate") || $("#start_date").val();
getURLParam = GetURLParameter("StartDate");    
selectedStartDate = (getURLParam ) ? getURLParam  : $("#start_date").val();

Why not just do something like this:

var urlParameter = GetURLParameter("StartDate");
selectedStartDate = urlParameter ? urlParameter : $("#start_date").val();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top