Saturday, 15 June 2013

Proper use of a comma in javascript ternary operator -



Proper use of a comma in javascript ternary operator -

rather utilize if else statement, i'm trying utilize ternary operator have syntax error somewhere in statement.

can tell me going wrong?

statement is:

my_alert(status ? ('accepted', 'alert-success') : ('declined', 'alert-info')) my_alert function has 2 parameters. status evaluates true or false. when pass more 1 parameter above expression, doesn't utilize of comma.

in chrome , firefox when function runs displays 'alert-success' or 'alert-info'. misses out first parameter.

i've looked on stackoverflow reply means it's telling me i'm doing correct.

any help great.

alternatively, wrap function phone call in ternary statement...

status ? my_alert("accepted", "alert-success") : my_alert("declined", "alert-info");

update:

robin van baalen makes suggestion...

my_alert.apply(this, status ? ["accepted", "alert-success"] : ["declined", "alert-info"]);

javascript ternary-operator comma-operator

No comments:

Post a Comment