如果语句只有Javascript中的数字

mic*_*pri 1 javascript type-coercion

我发现这个函数将数字分成几部分,我试图找出一切意味着什么.有一点我无法弄清楚.

这是代码:

function reduce(numerator,denominator) {
  var gcd = function gcd (a,b) {
    if (b) {
      return gcd(b, a%b);
    } else {
      return a;
    }
  };
  gcd = gcd(numerator,denominator);
  return [numerator/gcd, denominator/gcd];
}
Run Code Online (Sandbox Code Playgroud)

什么if (b)意思.我知道如果if语句中只有变量,则检查变量是true还是false.这将如何适用于一个数字?什么时候会转到else语句?

Pau*_* S. 7

这是做的事情是如何被转化为布尔,即无论什么是truthy与否

if (0 || NaN || undefined) {                // these are "falsy"
    // this never happens
} else if (1 /* or any other number*/ ){    // these are "truthy"
    // this happens
}
Run Code Online (Sandbox Code Playgroud)