Is there a way to execute the following code, without specifying 'a' twice?
const result = a > b ? a : default;
Useful for when the ternary is inside an arrow function and 'a' is an arithmetic operation the uses the function parameters:
someFunc((value) => value + otherValue / 10 > b ? value + otherValue / 10 : default)
Remember that 'a' can be a highly complex calculated expression.
The only way I know to write value + otherValue / 10
once is as follows:
someFunc((value) => {
const result = value + otherValue / 10;
return result > b ? result : default;
});
I'm looking for something like:
const result = a ?? > b : default;
PS. I know there's nothing wrong with the last example, just asking to figure out if JavaScript has a better way of doing this.