1

Little question, maybe it's a basic one .... but

What should be the value of ub ?

Suppose that getuploadedBytes return an Int, the value of ub should suppose to be what ?

The min or max value between method and right operator ?

ub = that._getUploadedBytes(jqXHR) || (ub + o.chunkSize);
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98
  • 1
    `0` is a falsy int. [`||` is OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR_()). No idea where you got min/max from. – Bergi Oct 18 '17 at 00:18
  • This is a duplicate of https://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation, https://stackoverflow.com/questions/6439579/what-does-var-foo-foo-assign-a-variable-or-an-empty-object-to-that-va, and many other questions – coagmano Oct 18 '17 at 00:23

2 Answers2

2

In summary, if that._getUploadedBytes(jqXHR) is non-zero, that value will be returned, otherwise (ub + o.chunkSize) is returned.

The || operator prefers the left value if it is truthy, and the right value otherwise.

Assuming that that._getUploadedBytes(jqXHR) returns an integer, then the only integer that is falsey is 0.

AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
  • 1
    Thank for the explication – Cédric Boivin Oct 18 '17 at 00:22
  • A better explanation would be that `||` is a [short circuit logical operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation), and short circuit logical operators evaluate left to right returning the first truthy value encountered. –  Oct 18 '17 at 00:51
  • @TinyGiant: I'm curious as to your reasoning behind why it's a better explanation? Personally, I prefer the least-technical explanation that is accurate. – AnilRedshift Oct 18 '17 at 22:13
2

The value will be the result of _getUploadedBytes unless that function returns 0, in which case it will be ub + o.chunkSize.

0 is a falsy value, which means the condition will fail and the other calculation will produce the result.

Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66