jeudi 7 juillet 2016

JavaScript - which If Else condition should I use?

There are different methods to use condition statements

  1. method:

    var x = 3,
        y;
    
        if (x == 4) y = 1;
        else if (x == 3) y = 2;
        else y = 3;
    
    alert(y);
    
  2. method:

var x = 3,
    y = x == 4 ? 1 : x == 3 ? 2 : 3;

alert(y);
  1. method:
var x = 3,
    y = x == 4 && 1 || x == 3 && 2 || 3;

alert(y);

My question now: Which method should I use? And why?

Aucun commentaire:

Enregistrer un commentaire