Switching between functions with a ternary operator

If we use a condition to choose which of two functions to call with the same parameters, we can use a ternary operator to remove some repetition.

// Regardless of which function we pick, we always pass 'martini' to it.
if (isJamesBond) {
} else {

// Because the parameter stays the same, we can select the function with a
// ternary operator instead.
(isJamesBond ? shake : stir)('martini')

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.