samedi 11 avril 2020

What syntax is best to use for an if statement with one piece of functionality inside?

For example:

if (e.keyCode === 13) {
  foo();
}

or

e.keyCode === 13 ? foo() : null;

I understand that this only applies to if statements with a single piece of functionality as a result, but for readability and to lessen the lines of code I would think the second example would make more sense. This is a curiosity I've always had and just wondering if there are any reasons to choose one style over the other. Thanks in advance for any responses!

Aucun commentaire:

Enregistrer un commentaire