When should one use the Javascript conditional operator? -


righto, have come across in lot of code have read , understand functional role of conditional operator in javascript; not understand when 1 should use it.

i have read ecmascript specification 5.1 on it, doesn't me great deal in deciding when impliment it.

for example, can quite see benefit of replacing code:

if (istrue == true) {   dosomething(); } else {   dosomethingelse(); } 

with next:

istrue ? dosomething() : dosomethingelse() 

where fall down when either code inside if... else... statement lengthy or conditional operator nested inside (an)other conditional operator(s). obvious candidate deciding when use version of conditional statement "does replacing conditional operator make code easier follow?". unfortunately rather inexperienced writing code , unsure of when , i'm biased understanding own code well, no matter how poorly written may be.

can more experience me give me less subjective test when you use it, can attempt follow suit?

related: js how use ?: (ternary) operator

ternary operator designed replace

if (test) {  = something(); } else {  = somethingelse(); } 

with

i = test?something():somethingelse(); 

you can't see if-then-else shortcut, conditional expression (not conditional instruction).


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -