1

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?

4 Answers 4

8
selectedStartDate = GetURLParameter("StartDate") || $("#start_date").val();
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

1

Why not just do something like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.