What is a simple way to test for equality in class instances?

I’m looking for a simple way to find equality of class instances where valueOf is a string.

Consider the code below. (codepen link) Some surprising results.

  • The class is a simple container holding a string. I’m comparing classes created from identical strings.
  • The less-than tests (1, 2) work great. It knows a is not less then b, and that a IS less than or equal to b.
  • Test 3 fails, but it does not call valueOf(). Okay.
  • Test 4 does call valueOf(), but still somehow does not see the two strings as equal.
  • Test 5 passes because I’m explicitly grabbing valueOf().

For test 3, I wish === called valueOf but okay. For test 4 I’m confounded. And test 1 and 2 show that it knows the values are equal. I’d like a simple way to test for equality here. Thoughts welcome.

class Container {
  #name;
  
  constructor(a) {
    this.#name = a;
  }

  valueOf() {
    console.log("valueOf() => "+this.#name);
    return this.#name;
  }
}

main();

function main() {
  const a = new Container("XYZ");
  const b = new Container("XYZ");
  test(1, a<b, false);                 // 1. PASS, a<b is false, (calls valueOf)
  test(2, a<=b, true);                 // 2. PASS, a<=b is true, (calls valueOf)
  test(3, a===b, true);                // 3. FAIL, a===b is false, (does *not* call valueOf)
  test(4, +a===+b, true);              // 4. FAIL, +a===+b is false, (*does* call valueOf ?!?)
  test(5, a.valueOf()===b.valueOf(), true); // 5. PASS, a.valueOf()===b.valueOf() is true
}

function test(n, a, x) {
  if (a === x) console.log(n + ". PASS");
  else console.log(n + ". FAIL: expected " + x);
  console.log("");
}