-4

I was just playing a code, and I found a little mysterious behavior with my code while turning the ternary operator into && and || operator:

let  a = 3;
a = a > 1 && 2 || a;
console.log(a);

let b = 3;
b = b > 1 ? 2 : b;
console.log(b);

// a and b both are same. It's fine.

const getAdjacentHighestProduct = (arr) => {
  let max = -Infinity;
  arr.reduce((a, c) => {
    let product = a * c;
    max = product > max ? product : max;
    return a = c;
  });
  return max;
};

const aProduct = getAdjacentHighestProduct([1, 0, 1, 0, 1000]);
console.log(aProduct);

const getAdjacentHighestProduct2 = (arr) => {
  let max = -Infinity;
  arr.reduce((a, c) => {
    let product = a * c;
    max = product > max && product || max;
    return a = c;
  });
  return max;
};

const aProduct2 = getAdjacentHighestProduct2([1, 0, 1, 0, 1000]);
console.log(aProduct2);

// aProduct, aProduct2 are not the same. It's not okay.

Also, here I can see the result is -Infinity for aProduct2. But while trying the same code in a test case, I am seeing the result is null, maybe it's due environment (browser vs node.js).

So, I am curious why both results are not the same and why is it returning -Infinity or null rather than 0?

So, what's exact difference between using a ternary operator and && || operator?

Here's the screenshot of the test result:

enter image description here

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231

3 Answers3

4

In case b is falsy, true ? b : c will be b whereas true && b || c will be c.

function ternary(a, b, c) {
  return a ? b : c;
}
function andOr(a, b, c) {
  return a && b || c;
}

console.log(ternary(1, 0, 3), andOr(1, 0, 3));
P Varga
  • 19,174
  • 12
  • 70
  • 108
  • *hang on are you also being sarcastic?* – P Varga Feb 15 '19 at 16:57
  • @JonasWilms If not sarcastic, then are you sure it's `null` and not `NaN`? – P Varga Feb 15 '19 at 17:07
  • It's `null`, that's what amazed me to post the question. – Bhojendra Rauniyar Feb 15 '19 at 17:10
  • I can't see how it can be `null`. Can you post an example that reproduces that? Because you omit the initial value in `reduce()`, it will be `let product = undefined * 1`, which is `NaN`, but not null – P Varga Feb 15 '19 at 17:12
  • 1
    @ııı if the second argument to `reduce` is omitted, the first iteration will be for the *second* value in the array, and the reduce "accumulator" will start off as the first value in the array. Thus the first iteration would have `a` as 1 and `b` as 0. – Pointy Feb 15 '19 at 17:15
  • @Pointy Oh, true. – P Varga Feb 15 '19 at 17:16
1

Thanks everyone! I got the answer. Let's break them here:

(I hope, you understand, don't think to give explanation)

0 > -Infinity && 0 || -Infinity
false > -Infinity && false || -Infinity
false > -Infinity

The null value was my key point of curiosity. Ah, I just found the right answer:

var a = {b: Infinity, c: 10};
console.log(JSON.stringify(a));

So, I think they use JSON.stringify to satisfy the test result.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
-2

The difference is: they do different things. ;)

Ternary Operator

x = c ? a : b

if c then
  x = a;
else
  x = b;
end if

Logical AND

x = a && b

Logical OR

x = a || b
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Mick
  • 954
  • 7
  • 17