dimanche 23 juillet 2017

javascript: using ternary operator for if/else if/else statement

I was looking at these if statements:

 if (cellDate < filterLocalDateAtMidnight) {
    return -1;
 } else if (cellDate > filterLocalDateAtMidnight) {
    return 1;
 } else {
    return 0;
 }

from this article and I was wondering why short hand if statements were not used (I.e. using the ternary operator)> is there a disadvantage to using it? It seems like a good opportunity to use it.

If it can be used, is the below how you would simplify it? I'd like to refactor my code although I'm a little worried about straying from the example in the article (and inadvertently introducing some special case glitches)

cellDate < filterLocalDateAtMidnight ? return -1 :(cellDate > filterLocalDateAtMidnight : return 1 : return 0 );

it seems i get an error with the above shorthand i wrote

Aucun commentaire:

Enregistrer un commentaire