EDIT: the previous solution has been removed according to your question
As you only need to understand how short-circuit evaluation of logical operator works, so I will add the following explanation:
I - Keypoints:
1- logical AND operator && returns the first falsy value, and if all operands were evaluated, it returns the last one.
2- logical OR operator || returns the first truthy value, and if all operands were evaluated, it returns the last one.
3- The precedence of AND && operator is higher than OR ||
4- Check the table list of JS operators precedence
In your example, both operands (arguments) of the logical OR are truth (strings).
II - Example:
Starting from your code, let's consider this object of values:
const game = {
odds: {
team1: 10,
team2: 3
}
}
const winner = game.odds.team1 < game.odds.team2 && `$team1 is more likely to win` || `team2 is more likely to win`
console.log(winner)
Here, winner will be assigned to the last operand. But why? Let's break-down the logic (remember the keypoints):
1- game.odds.team1 < game.odds.team2 is false and $team1 is more likely to win is truthy as it's a string, then && will return the first falsy value.
2- game.odds.team1 < game.odds.team2 is returned (still false). and it's evaluated now with team2 is more likely to win which is a truthy string.
3- the logical OR return the first truthy value, therefore team2 is more likely to win is assigned to winner.
I hope it's clear now