# Return Early Pattern for Functions OR Comparison

Tell us what’s happening:

Hey All,
This seems to work, but earlier today, I used:

``````  // Only change code below this line
if (a  || b < 0) {      // <<<<<<<<<<<<  Note the change here
return undefined;
} else {
// Only change code above this line
``````

and as you guessed, it did not work. Should this work? In my head, it was logical and I was attempting to write as little code as possible.

``````
// Setup
function abTest(a, b) {
// Only change code below this line
if (a < 0  || b < 0) {
return undefined;
} else {
// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
}
// Change values below to test your code
abTest(2,2);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36`.

Thanks!

Dave

Boolean logic doesn’t work like that (this isn’t a Javascript-specific thing, it’s a logic thing). What that code is saying is:

``````IF a
OR IF b < 0
``````

So for `abTest(2, 2)`, you are saying:

``````IF 2
OR IF 2 < 0
``````

And what does `IF 2` mean? Not anything, not really, but JavaScript, in an attempt to be helpful and not blow up, coerces anything in that `if` statement to true or false. JS treats any number that isn’t 0 as `true`. So the first logical check is `IF true`, which is true. Note that many other languages would not coerce it, and so in those languages the condition throw an error because `2` is not a boolean value (and therefore cannot be tested using boolean logic).

But

``````if (a < 0  || b < 0)
``````

is saying

``````IF a < 0
OR IF b < 0
``````

Which makes sense in this context

1 Like

Thanks @DanCouper, this is exactly what I needed. Thanks for making it click.

Dave

1 Like