javascript - How to write in shorthand form if / else if / else? -
is there shorthand if / else if / else statement? example, know there's 1 if / else statement:
var n = $("#example div").length; $("body").css("background", (n < 2) ? "green" : "orange");
but how write following in shorthand syntax above?
var n = $("#example div").length; if (n < 2) { $("body").css("background", "green"); } else if (n > 2) { $("body").css("background", "blue"); } else { $("body").css("background", "orange"); }
it exist it's highly un recommended because it's pretty hard read , maintain.
var n = $("#example div").length, color; color = (n < 2) ? 'green' : (n > 2) ? 'blue' : 'orange'; $("body").css("background", color);
Comments
Post a Comment