What is the exact difference between the ternary operator and && operator?

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;

let b = 3;
b = b > 1 ? 2 : 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]);

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]);

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

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));

Difference between the Ternary operator and Null coalescing , Ternary operatorTernary operator is used to replace if else statements into one statement.Syntax(condition) ? expression1 : expression2� Conditional (ternary) operator. The question was not looking for this but thought it would make sense to pop it in as its another case where the ? can be seen in use. When seen in typescript you can use it in a conditional ternary statement (if / else) looking like so.

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};

What is the difference between the ternary operator and if-else , What is the difference between the ternary operator and if-else statements in Java? 3 Answers What's the difference between println() and printf() in Java? Ternary Operators. Now that we’ve taken a look at how if / else statements work, let’s take a look at the ternary operator.The ternary operator takes 3 different arguments which include the

The difference is: they do different things. ;)

Ternary Operator
x = c ? a : b

if c then
  x = a;
  x = b;
end if
Logical AND
x = a && b
Logical OR
x = a || b

If / Else Statements vs Ternary Operators in JavaScript, Comparing If / Else statements with Ternary Operators in JavaScript as key differences between if / else statements and ternary operators as well and ternary operators as they can appear to be the same exact things that� The behavior of the ternary operator was an attempt to be more realistic in the propagation of unknowns during simulation. With the ternary operator, it is easy to define its evaluation behavior since the true and false branches must produce a result with the same type. An if/else statement is more general; the code in the true and false

The Ternary Operator vs The Conditional Operator WTF - DEV, I have done my absolute best job to put and end to this discussion. Not for your benefit. See I don't care about you at all, I just want to get this� Difference between == and === operator in JavaScript The ‘==’ operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison. But the ‘===’ operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it

Conditional (ternary) operator, The conditional (ternary) operator is the only JavaScript operator that takes This operator is frequently used as a shortcut for the if statement. Ternary Operator – Ternary operator provides a shorthand way to write all the above conditional statements. If the condition is not very complex, its better to use ternary operator to reduce the code size but for more complex conditions, it can become confusing.

ternary conditional operator - ?:, In computer programming, ?: is a ternary operator that is part of the syntax for basic The difference is significant if evaluating the expression has side effects. an actual conditional operator was introduced, using the If keyword instead of IIf . Note that the "ternary operator" is better described as the "conditional operator". The former name merely notes that it has three arguments without saying anything about what it does. Needless to say, if PHP picked up any more ternary operators, this will be a problem.