Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

429
Views
Comparing Custom Strings(classes extending String class) using ==

I was wondering, if there are any ways on how to compare 2 custom string objects which are instances of classes which extend String, using == doesn't work and it always returns false.

I have tried using valueOf but with no luck, the method gets never called if both operands are objects.

See an example below

class MyString extends String {
  doSomething(someData) {
    return doSomethingWithData(this, someData);
  };

  valueOf() {
    console.log('valueOf called');
    return this.toString();
  };
};

When doing console.log(new MyString('hi') == 'hi');, I Get

valueOf called
true

But When doing console.log(new MyString('hi') == new MyString('hi'));, I Get false.

So, the valueOf gets never called and the comparison blindly returns false, is there any possible way to fix it?

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!