?: Operator
If you want to shorten if/else
you can use ?:
operator as:
condition ? action-on-true : action-on-false(else)
For instance:
let gender = isMale ? 'Male' : 'Female';
In this case else
part is mandatory.
I see this kinda short hand format use for null
/undefined
checking like:
const avatarName = user.firstname ? user.firstname : user.lastname;
However in this case you can simply use:
const avatarName = user.firstname ?? user.lastname;
or
const avatarName = user.firstname || user.lastname;
To see the difference check this out
&& Operator
In another case, if you have only if
condition you can use &&
operator as:
condition && action;
For instance:
!this.settings && (this.settings = new TableSettings());
FYI: You have to try to avoid using if-else or at least decrease using it and try to replace it with Polymorphism or Inheritance. Go for being Anti-If guy.