selectedStartDate = GetURLParameter("StartDate") || $("#start_date").val();
Ternary if in Javascript with function
-
04-07-2022 - |
Question
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?
Solution
OTHER TIPS
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow