# Basic JavaScript - Return Early Pattern for Functions - Difference between (a <0 || b<0) and ((a || b) < 0)

Tell us what’s happening:
Describe your issue in detail here.

Hi,

can anybody tell me about the difference between:

``````if (a <0 || b<0) {
return undefined;
}
``````

and

``````if ((a || b) < 0) {
return undefined;
}
``````

?

thank you

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

// Only change code above this line

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}

abTest(2,2);
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:103.0) Gecko/20100101 Firefox/103.0`

Challenge: Basic JavaScript - Return Early Pattern for Functions

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

They are different because the do different things:

``````const check1 = (a, b) => (a <0 || b<0)
const check2 = (a, b) => ((a || b) < 0)

for (let a = -1; a <= 1; a++) {
for (let b = -1; b <= 1; b++) {
const c1 = check1(a, b)
const c2 = check2(a, b)
console.log('a =', a, 'and b =', b)
console.log('the two checks return:', c1, c2)
if (c1 !== c2) {
console.log('*** They are not equal.')
}
}
}
``````

Returns:

``````a = -1 and b = -1
the two checks return: true true
a = -1 and b = 0
the two checks return: true true
a = -1 and b = 1
the two checks return: true true
a = 0 and b = -1
the two checks return: true true
a = 0 and b = 0
the two checks return: false false
a = 0 and b = 1
the two checks return: false false
a = 1 and b = -1
the two checks return: true false
*** They are not equal.
a = 1 and b = 0
the two checks return: false false
a = 1 and b = 1
the two checks return: false false
``````

They are not the same.

Why?

You are asking two different things. The first is asking if a is less than 0 AND if b is less than 0. The second one is asking if the first non-falsy value of a and b is less than 0.

Remember that with JS numbers, 0 (and -0 and NaN) is falsy and everything else is truthy. And remember that in JS, the `||` really works as a selector. If the first value is truthy, the whole thing evaluates to the first value. If the first value is falsy, the whole things evaluates to the second value. Really, it is the equivalent of `a ? a : b`.

So, why does that one fail?

This is where a = 1 and b = -1,

The first logic:

``````a < 0 || b < 0
1 < 0 || -1 < 0
false || true
true
``````

The second logic:

``````(a || b) < 0
(1 || -1) < 0
1 < 0
false
``````
1 Like

Hola Kevin, thank you very much for answering.
I hope your Spanish is getting as good as your math logic!
Best,
P

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.