aBloomer aBloomer - 19 days ago 7
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.

Answer

Use the ternary operator:

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