January 2020
Intermediate to advanced
548 pages
13h 36m
English
Often, there's a need to express more complex conditions in each case, instead of just matching a singular value. If we pass true as SwitchExpression, then we are free to express custom conditional logic within each CaseExpression, as long as each CaseExpression evaluates to true when successful:
switch (true) { case user.role === 'admin' || user.role === 'root': { // ... break; } case user.role === 'member' && user.isActive: { // ... break; } case user.role === 'member' && user.isRecentlyInactive: { // ... break; }}
This pattern allows us to express more multivariate and hybrid conditions. You may usually feel inclined toward multiple if/else/if/else statements, but if your logic can be expressed in a switch statement, ...
Read now
Unlock full access