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

141
Views
Strict or Loose equality when comparing to a Symbol?

Is strict equality (===) ever required when comparing any entity to a Symbol? None of the type conversion rules on MDN indicate any conversion to/from a Symbol and a reading of them indicates that it should always be OK (apart from perhaps spoiling code uniformity?) to use == when one of the entities is a Symbol.

So can something like

function foo(x) {
   return x == somePredefinedSymbol;
}

ever go wrong, because as far as I understand the above equality can only return true if x is somePredefinedSymbol?

P.S.: This is just for technical curiosity. I don't intend to advocate the usage of ==.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So can something like

function foo(x) {
   return x == somePredefinedSymbol;
}

ever go wrong,

Yes, this is a somewhat constructed example but it does showcase a failure scenario:

const predefinedSymbol = Symbol("foo");

const someObject = {
  value: "bar", // some unrelated values
  [Symbol.toPrimitive]() { // override primitive conversion
    return predefinedSymbol;
  }
}

console.log(someObject ==  predefinedSymbol); //true
console.log(someObject === predefinedSymbol); //false

As of ES6 the loose equality will take account of Symbols when compared to an object. From ECMAScript 6 specification, 7.2.12 Abstract Equality Comparison:

  1. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then
    return the result of the comparison x == ToPrimitive(y).
  2. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then
    return the result of the comparison ToPrimitive(x) == y.

Therefore, if an object converted to a primitive produces a symbol and that is the same symbol you compare with, then loose equality will produce true but using strict equality the result be false.

about 4 years ago · Juan Pablo Isaza Report
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!