Hiprup

What is the output of console.log(1 < 2 < 3) and console.log(3 > 2 > 1)?

They log true and false — and that surprises people. Comparisons evaluate left to right, and each boolean result coerces to a number (true→1, false→0).

  • 1 < 2 < 3 — (1 < 2) is true → true < 3 becomes 1 < 3 → true.

  • 3 > 2 > 1 — (3 > 2) is true → true > 1 becomes 1 > 1 → false.

Lesson: comparison operators don't chain mathematically — never write a < b < c expecting a range check.

console.log(1 < 2 < 3);  // true
// Step 1: (1 < 2) = true
// Step 2: true < 3 → 1 < 3 = true

console.log(3 > 2 > 1);  // false!
// Step 1: (3 > 2) = true
// Step 2: true > 1 → 1 > 1 = false

// More examples
console.log(true + true);     // 2 (1 + 1)
console.log(true + false);    // 1 (1 + 0)
console.log(false + false);   // 0 (0 + 0)
console.log(true == 1);       // true
console.log(false == 0);      // true

// Correct chained comparison (JS does not support Python-style)
const x = 5;
console.log(1 < x && x < 10); // true (explicit AND)

JS evaluates left-to-right: (3 > 2) = true, then true > 1 coerces true to 1, and 1 > 1 is false. This is a coercion trap.

Boolean true = 1, false = 0 in numeric context. For chained comparisons, use explicit && (1 < x && x < 10).

Walk through step-by-step: left-to-right evaluation, boolean-to-number coercion (true=1, false=0). This is a classic output question.

Show that JS does not support chained comparisons like Python — use && instead.

What is the output of console.log(1 < 2 < 3) and console.log(3 > 2 > 1)? | Hiprup