Frontend Development 9 min read

How to Make the Expression x == 1 && x == 2 && x == 3 True in JavaScript

This article explains how to satisfy the JavaScript expression x == 1 && x == 2 && x == 3 by understanding the differences between loose (==) and strict (===) equality, type coercion rules, and implementing custom valueOf or toString methods on objects to increment a counter during each comparison.

政采云技术
政采云技术
政采云技术
How to Make the Expression x == 1 && x == 2 && x == 3 True in JavaScript

During an interview the interviewer asked how to make the expression x == 1 && x == 2 && x == 3 evaluate to true. The answer lies in the behavior of JavaScript's loose equality operator (==) and the type‑conversion rules defined in the ES5 specification.

Loose vs. strict equality

For primitive types, == performs type conversion before comparing values, while === returns false if the types differ.

For reference types, both operators compare the underlying pointer address.

When a primitive is compared with an object, == converts the object to a primitive using ToPrimitive before the comparison.

The specification defines how Type(x) influences the result. For example, undefined and null are equal to each other, NaN is never equal to anything, and numeric and string comparisons involve converting the string to a number.

Equality between strings and numbers

If Type(x) is Number and Type(y) is String, the comparison is performed as x == ToNumber(y) . The reverse holds when x is a string and y is a number.

var a = 42;
var b = "42";
a === b; // false
a == b; // true

Boolean values are first converted to numbers (true → 1, false → 0) before comparison with other types.

var x = true;
var y = "42";
x == y; // false (1 == 42)

Object vs. non‑object comparison

When an object is compared with a primitive, JavaScript calls the object's valueOf and, if necessary, toString to obtain a primitive value (the ToPrimitive operation).

toPrimitive() function

The toPrimitive(input, preferredType) algorithm is used internally to convert objects to primitive values. If preferredType is number , valueOf is tried first; if it fails, toString is used. For string preference the order is reversed.

Making the expression true

By defining an object x with a mutable property val and overriding either valueOf or toString to increment val and return the new value, each equality check yields the next integer (1, then 2, then 3), satisfying the chain.

const x = {
  val: 0,
  valueOf() {
    this.val++;
    return this.val;
  }
};
// x == 1 && x == 2 && x == 3 evaluates to true

The same effect can be achieved by overriding toString instead of valueOf . The article concludes with a reference to the ES5 specification and further reading.

JavaScriptType ConversionEqualityLoose EqualitytoPrimitivetoStringvalueof
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.