jquery - javascript ternary condition operation -
i want achieve below logic using javascript ternary operation. possible?
if(condition1){ console.log("condition1 pass"); } else if(condition2){ console.log("condition2 pass"); } else{ console.log("it different"); }
sure, have have 1 ternary inside another:
put else if
condition inside :
part.
console.log( condition1 ? "condition1 pass" : condition2 ? "condition2 pass" : "it different" );
it's best not syntax mistakable , slim, could, however, move function or iife if it's inside function , return result directly reduce loop:
function testcondition(condition1, condition2){ if(condition1){ return "condition1 pass"; } else if(condition2){ return "condition2 pass"; } return "it different"; } var conditionalpass = (function(condition1, condition2){ if(condition1){ return "condition1 pass"; } else if(condition2){ return "condition2 pass"; } return "it different"; })(condition1, condition2);
Comments
Post a Comment