Refactoring Conditional Adding Property to Object
Instead of conditionally adding a property using a Ternary. Use the "||" operator π So if the 1st expression can be converted to true, use that. Otherwise, use the 2nd expression π
const refill = {};
// Ternary
refill['π₯€'] = refill['π₯€'] ? refill['π₯€'] + 1 : 1;
// β
Refactor using ||
refill['π₯€'] = refill['π₯€'] + 1 || 1;
What is this Code doing?
Let me explain this in words what this code is doing:
- If the property
'π₯€'
exists in the object, then add 1 - If the property doesn't exist, create that property and set the value as 1
Alright, let's look at the output of the individual scenario:
1. Property doesn't exist
const refill = {};
refill['π₯€'] = refill['π₯€'] + 1 || 1;
console.log(refill);
// OUTPUT β‘οΈ { 'π₯€': 1 }
2. Property does exist
const refill = { 'π₯€': 1 };
refill['π₯€'] = refill['π₯€'] + 1 || 1;
console.log(refill);
// OUTPUT β‘οΈ { 'π₯€': 2 }
Rewriting this Ternary with if
In case you're unfamiliar with the ternary operation, let me re-write this code with a if
statement. Hope this will make this code a bit more digestible π
const refill = {};
if (refill['π₯€']) {
refill['π₯€'] = refill['π₯€'] + 1
} else {
refill['π₯€'] = 1
}
Explaining the ||
Operator
For the longest time, I thought the ||
operator was only used in conditional. Until I read Kyle Simpson's "You Don't Know JS" book - I highly recommend reading his book! Did I mention that it's FREE π
Kyle explains ||
as selector operators instead of logical operators:
Why? Because they don't actually result in logic value (aka
Boolean
).
So what do they result in? They result in the value of one (and only one) of their two operands. In other words, they select one of the two operand's values.
That means you can use it as a value assignment. Whoa, isn't that cool π€© And that's exactly what I'm doing in this code tidbit π
This pattern is quite often used for setting default values. If this is the first time you've seen this pattern, it helps thinking of it in terms of ternary. At least, it was helpful for me at the beginning of understanding it π
'Option A' || 'default option';
// This is roughly the same as
'Option A' ? 'Option A' : 'default option';
β οΈ BUT! Before you go crazy and refactor all ternary statements with the ||
operator. Read the next section!
||
Does NOT Replace Ternary Operator!
Although the ||
operator is a very useful patter to set default values. It does NOT replace the ternary. You should only use it in situations where you want ALL falsy values to be skipped. Otherwise, it's better to use a ternary instead where you can be more explicit.
An example where Ternary is better than ||
Let's look at a slightly different version of our refill example. Here's a function of creating our refill object. But now, we allow our users to skip the refill. Meaning they want 0
refills.
function refillDrink(num) {
const refill = {};
refill['πΊ'] = num || 1;
return refill;
}
refillDrink(0);
// π± This is NOT what we want >> { 'πΊ': 1 }
βοΈ Oh no! We can't pass in 0
. That's because 0
is a falsy value. In the situation, a ternary would be much better.
function refillDrink(num) {
const refill = {};
refill['πΊ'] = num !== undefined ? num : 1;
return refill;
}
refillDrink(0);
// β
{ 'πΊ': 0 }
βοΈ Yay, we can set 0
as our value π
Top comments (3)
I'm sorry, but the icon for key is hard to read :(
I used icon in my code tidbit, so I thought I should be consistent with the actual code examples. Maybe for the samples, I'll use text instead. Thanks for the feedback π
Its ok :D its fun to use icons