0

The code is as follows.

this.requiredVehicle = order.vehicle
      ? order.vehicle
      : order.required_vehicle_type && order.required_vehicle_type;

If vehicle is present in the order, that value should be assigned else if required_vehicle_type is present, it should be assigned. How to simplify the above code ?

prajeesh
  • 2,202
  • 6
  • 34
  • 59
  • `??`? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator – General Grievance Feb 22 '21 at 13:36
  • 2
    `order.vehicle || order.required_vehicle_type` or `??` – adiga Feb 22 '21 at 13:36
  • You could do `this.requiredVehicle = order.vehicle || order.required_vehicle_type || null` – secan Feb 22 '21 at 13:36
  • @secan The `|| null` is probably superfluous, unless you have specific requirements about the value's type… – deceze Feb 22 '21 at 13:37
  • 1
    @deceze, yes, you are right; it is not strictly necessary but, in a case like this, I always tend to explicitly set something to `null` rather than leaving it undefined. If something is `null`, you know "it exists" and someone took it into consideration and explicitly decided it should have no value; if it is `undefined` you always wonder if it exist and whether it is `undefined` because of an error or because of an explicit choice. – secan Feb 22 '21 at 13:54

0 Answers0