Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The abstract equality operator (==) evaluates whether two operands are equal, performing implicit type coercion before making the comparison if the operands are of different data types. Unlike the strict equality operator (===), it checks for value equivalence rather than strict type-and-value identity.
operand1 == operand2
When evaluating x == y, the JavaScript engine executes the Abstract Equality Comparison Algorithm. This algorithm dictates a specific sequence of type conversions based on the types of the operands: 1. Identical Types If both operands are of the same type, no coercion occurs. The operator behaves exactly like strict equality (===).
42 == 42;        // true
'text' == 'text'; // true
2. Null and Undefined The null and undefined types are loosely equal to each other. Generally, they are not loosely equal to any other value, with the specific exception of objects implementing the [[IsHTMLDDA]] internal slot (most notably document.all in browser environments). These specific objects evaluate to true when loosely compared to null or undefined.
null == undefined;    // true
null == 0;            // false
document.all == null; // true (in browser environments)
3. Strings, Numbers, and BigInts If one operand is a String and the other is a Number, the engine attempts to convert the String to a Number using the internal ToNumber() operation. If a String is compared to a BigInt, the String is converted to a BigInt. When a Number and a BigInt are compared, they are evaluated based on their exact mathematical values.
'42' == 42; // true  (String '42' becomes Number 42)
'' == 0;    // true  (Empty string becomes Number 0)
1n == 1;    // true  (Mathematical values are equal)
'1' == 1n;  // true  (String '1' becomes BigInt 1n)
4. Booleans If either operand is a Boolean, the Boolean is coerced into a Number before any further comparison is made. true is converted to 1, and false is converted to 0.
true == 1;   // true  (true becomes 1)
false == 0;  // true  (false becomes 0)
true == '1'; // true  (true becomes 1, then String '1' becomes Number 1)
5. Objects and Primitives If one operand is an Object (including Arrays and Functions) and the other is a primitive (String, Number, BigInt, or Symbol), the Object is converted to a primitive value using the internal ToPrimitive() operation. The engine first attempts to invoke the object’s [Symbol.toPrimitive]() method. If that method is not present, it falls back to invoking the object’s valueOf() and/or toString() methods.
[9] == 9;                // true  ([9] becomes '9', then becomes Number 9)
'[object Object]' == {}; // true  ({} becomes '[object Object]')

Technical Exceptions and Edge Cases

  • NaN (Not-a-Number): The NaN value is never equal to any value, including itself, regardless of the operator used.
NaN == NaN; // false
  • Object Reference Equality: When both operands are Objects, no coercion occurs. The == operator checks for reference equality, meaning it only returns true if both operands reference the exact same object in memory. It does not perform structural or deep equality checks.
[1, 2] == [1, 2]; // false (different memory addresses)
let a = {};
let b = a;
a == b;           // true  (same memory address)
Master JavaScript with Deep Grasping Methodology!Learn More