I am relatively new to Javascript and I was trying to understand the use of || in the current code.
So, I was going through this amazing article on web about reduce in Javascript where he wrote this example
const fruitBasket = ['banana', 'cherry', 'orange', 'apple', 'cherry', 'orange', 'apple', 'banana', 'cherry', 'orange', 'fig' ];
const count = fruitBasket.reduce( (tally, fruit) => {
tally[fruit] = (tally[fruit] || 0) + 1 ;
return tally;
} , {})
count // { banana: 2, cherry: 3, orange: 3, apple: 2, fig: 1 }
[Question] Here, I am unable to understand two things
tally[fruit] = (tally[fruit] || 0) + 1 ;
What is this ||
doing here and why did he used it? (I know this is an or operator)
And secondly what does this mean , {})
at the end of function?