aBloomer aBloomer - 2 months ago 11
Javascript Question

Is any better way to refactor this code?

Below is my code

if(props.plan[plan_id]) {
if(props.plan[plan_id].number== 0) {
return null;
else {
return 'some value';

First of all, I need to check if prop.plan[plan_id] is exist then use its data.
Is any way can make this code more beautiful because I wrote if statement twice.


Use the ternary operator:

if (props.plan[plan_id])
   return (props.plan[plan.id].number == 0) ? null : 'some value';