> ## 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.

# TypeScript Loose Equality

The `==` (abstract equality) operator evaluates whether two operands are equal by applying the ECMAScript Abstract Equality Comparison algorithm. Unlike the strict equality operator (`===`), if the operands are of different data types, the JavaScript runtime implicitly converts (coerces) them to a common type before performing the comparison.

## Type Coercion Mechanics

When evaluating `x == y`, the runtime applies the following primary coercion rules:

* **Null and Undefined:** `null` and `undefined` are considered loosely equal to each other, but not to any other values.
* **String and Number:** If one operand is a string and the other is a number, the string is converted to a number via the internal `ToNumber` operation.
* **Booleans:** If either operand is a boolean, it is converted to a number (`true` becomes `1`, `false` becomes `0`) before further comparison.
* **BigInt:** If a `bigint` is compared to a `number`, they are compared by their mathematical value without coercion. If a `string` is compared to a `bigint`, the string is converted to a `bigint` using the internal `StringToBigInt` operation.
* **Objects and Primitives:** If one operand is an object and the other is a primitive (string, number, bigint, or symbol), the object is converted to a primitive using its internal `ToPrimitive` method (typically invoking `valueOf()` or `toString()`).

## Syntax

```typescript theme={"dark"}
operand1 == operand2
```

## TypeScript Compiler Behavior

While TypeScript emits standard JavaScript `==` at runtime, its static type checker restricts abstract equality comparisons during compilation. If the TypeScript compiler determines that the types of the two operands have no overlap, it will throw a `ts(2367)` error, preventing the comparison of mutually exclusive types. To trigger runtime coercion in TypeScript without compiler errors, at least one operand must be typed as `any` or `unknown`, or the types must share a union.

```typescript theme={"dark"}
let num: number = 5;
let str: string = "5";
let anyVal: any = "5";

// Runtime coercion (Allowed via 'any' type bypass)
console.log(num == anyVal); // Evaluates to true: "5" is coerced to 5

// Compile-time error
// TS2367: This condition will always return 'false' since the types 'number' and 'string' have no overlap.
// @ts-expect-error
console.log(num == str); 
```

## Evaluation Order

1. If the operand types are identical, the operator performs a strict equality comparison (identical to `===`).
2. If the operand types differ, the runtime attempts type coercion based on the ECMAScript specification.
3. The newly coerced values are compared.
4. The expression resolves to a boolean (`true` or `false`).

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
