0

I am having some troubles to understand what this code do. Its a simple operation but i am confused with the result

$scope.diff = ($scope.diff || 0) - (e.model.ContainerReturnedNo || 0) + (e.values.ContainerReturnedNo || 0)

What is that $scope.diff || means?
Giving numbers like $scope.diff = undefined, e.model.ContainerReturnedNo = 3 and e.values.ContainerReturnedNo = 4 the result is 4. Can any one explain to me please? This is AngularJS with kendo UI.

Thanks

Michael W. Czechowski
  • 3,366
  • 2
  • 23
  • 50
pant
  • 30
  • 1
  • 6
  • 3
    Possible duplicate of [JavaScript OR (||) variable assignment explanation](https://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation) – Maak Feb 22 '18 at 15:00
  • Actually yes it is but i didn't find it before my post. Sorry for that – pant Feb 22 '18 at 15:08

1 Answers1

1

It's the same as everywhere else in javascript.

var test = something || 0;

If something is truthy, test will be something. If something is falsy, it will be 0.

This is because in javascript, the || operator return the first thruthy operand.

See JavaScript OR (||) variable assignment explanation for more.

Magus
  • 14,796
  • 3
  • 36
  • 51