-1

My code is failing here at this part ..i cant understand what is the role of this && operator in js?

    export function calculateLineTotals(line, invoice) {

    let totals = _calculateLineTotals(line, invoice)
    return {
    amount: totals.amount && 
        totals.amount.decimalPlaces(2).toNumber(),
    vatRate: totals.vatRate && 
        totals.vatRate.decimalPlaces(2).toNumber(),
    grossAmount: totals.grossAmount && 
        totals.grossAmount.decimalPlaces(2).toNumber(),
    vatAmount: totals.vatAmount && 
        totals.vatAmount.decimalPlaces(2).toNumber(),
    }
Malek
  • 85
  • 8
  • 4
    Read the JavaScript documentation regarding the [logical operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description). – axiac May 23 '19 at 14:42
  • 2
    "My code is failing here" - can you be more specific about how it's failing? Do you have an error message in your browser console? – Rup May 23 '19 at 14:42
  • You can read it like: `if totals.amount is truthy`, then I want it to get `totals.amount.decimalPlaces(2).toNumber()` otherwise it will be `undefined` – Icepickle May 23 '19 at 14:43

1 Answers1

0
totals.amount && totals.amount.decimalPlaces(2).toNumber()

totals.amount will evaluate to the value

&& will evaluate the right handside expression if lefthandside evaluates to true (not null or undefined or 0)

i guess thats what you want to know

this eliminates possibility of cant evaluate 'decimalPlaces' of undefined error

Nikko Khresna
  • 862
  • 5
  • 9