0

Edit: This is not a duplicate! This is specifically asking about the combination of an if statement with the Ternary Operator. The other threads do not address this, nor is it asked there as far as I can tell. The confusing part for me was that the Ternary Operator replaces one of two if statements where one if statement is inside another one.

My question also contains sub questions (which may or may not be ideal) that also weren't being addressed in the other threads.

Here's the original question: Can someone help me understand how this code works, please?

I found it on this site. Here: Calculate the LCM of two or three numbers in JavaScript

function gcd2(a, b) {
    // Greatest common divisor of 2 integers
    if(!b) return b===0 ? a : NaN;
    return gcd2(b, a%b);
}

Firstly, I thought the ternary operator was supposed to be used instead of an if function. Here they're used together?

The way I read the code is as follows:

if b is undefined, the if statement returns "false" (because b===0 is false when b is undefined), but it also assigns "a" to something, but to what? And why? Does "return b===0" end the function? And then it assigns NaN to something when b is not undefined. Why?

I hope to understand the rest of the code once I understand the first part.

camelCase
  • 3
  • 4
  • It's not doing an assignment. `return` ends the function and provides the value to the caller. The value is then *either* `a` *or* `NaN` depending on whether `b` is `0` or not – VLAZ Oct 15 '20 at 09:48
  • 2
    `if(!b) return b===0 ? a : NaN;` is the same as `if(!b) { if (b === 0) { return a; } else { return NaN; } }` – Andreas Oct 15 '20 at 09:49
  • The [conditional operator (`? :`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) is an alternative to `if..else` where the blocks are single statements. – RobG Oct 15 '20 at 10:09
  • @Andreas, thanks, that helps a little. So the first if means "if b is undefined", right? So if b is undefined, why does it then ask if b===0 ? How can it be 0 when it is undefined? – camelCase Oct 15 '20 at 10:19
  • @VLAZ, thanks, that helps too, so same question as I asked Andreas, I guess. So return ends the function if b is undefined (!b), then how could b at the same time be 0 or something else? – camelCase Oct 15 '20 at 10:24
  • 1
    `if (!b) { ... }` tests if `b` is [falsy](https://developer.mozilla.org/de/docs/Glossary/Falsy) not if it is `undefined`. Falsy values are `undefined`, `null`, ``, `NaN` and `0`/`-0` - hence the test for `b === 0` – Andreas Oct 15 '20 at 10:29
  • 1
    @camelCase `!b` will check if `b` is *falsy*. Both `undefined` and `0` are falsy. Check [All falsey values in JavaScript](https://stackoverflow.com/q/19839952) – VLAZ Oct 15 '20 at 10:32
  • @Andreas Thank you so much! Your combined answers helped me understand it. This is awesome! Someone in another thread suggested that "if (!x)" means undefined. That's one of the reasons I didn get it. Should have checked the validity of that first. – camelCase Oct 15 '20 at 13:03
  • @VLAZ Thank you so much! Your combined answers helped me understand it. This is awesome! Someone in another thread suggested that "if (!x)" means undefined. That's one of the reasons I didn get it. Should have checked the validity of that first. – camelCase Oct 15 '20 at 13:03
  • "Caps for emphasis, not screaming"—I suggest you use _Markdown_ for emphasis, not caps: `_Markdown_`. – ChrisGPT was on strike Oct 15 '20 at 18:26
  • @Chris Done! Of course, should have just used *emphasis* – camelCase Oct 15 '20 at 22:03

0 Answers0