Home > Enterprise >  javascript single line condition
javascript single line condition

Time:10-01

I have assignment of values below where value is based on computations for example in the code below.

If the result of

(this.dealDispositionFormFields.terminationPayment / this.metricsData["remainingObligation"])

is not a number or infinity then assign the value of this.terminationPaymentPercentageOfTotalObligation to 0

How do we address that in a one line statment in javascript ? Thanks,

image

#sample code

this.terminationPaymentPercentageOfTotalObligation 
 = ( this.dealDispositionFormFields.terminationPayment 
     / this.metricsData["remainingObligation"] );

this.terminationPaymentPercentageOfTotalObligation 
  = Math.round(
    this.metricsData["terminationPaymentPercentageOfTotalObligation"]
    * 100);

CodePudding user response:

I think you're looking for a conditional operator.

var x
if(condition) {
  //Do something
  x=1
} 
else {
  //Do something else 
  x=2
}

is equivalent to

const x = condition ? 1 : 2

  • Related