aBloomer aBloomer - 1 year ago 68
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 Source

Use the ternary operator:

if (props.plan[plan_id])
   return (props.plan[plan.id].number == 0) ? null : 'some value';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download