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

The `!=` (loose inequality) operator evaluates to `true` if its two operands are not equal, performing automatic type coercion before comparison if the operands are of different types. It is the negated counterpart to the `==` (loose equality) operator and contrasts with the `!==` (strict inequality) operator, which does not perform type conversion.

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

## Evaluation Mechanics

When evaluating `operand1 != operand2`, the JavaScript runtime underlying TypeScript applies the Abstract Equality Comparison algorithm. The mechanics are as follows:

1. **Identical Types:** If both operands are of the same type, no coercion occurs. The operator behaves identically to `!==`.
2. **NaN Behavior:** The `NaN` (Not-a-Number) value is never equal to any value, including itself. Therefore, `NaN != NaN` always evaluates to `true`.
3. **Nullish Operands:** `null` and `undefined` are considered loosely equal to each other, but not to any other values. Therefore, `null != undefined` evaluates to `false`. Comparing any object to `null` or `undefined` evaluates to `true` without triggering any type conversion.
4. **Primitive Coercion:** If comparing a `number` and a `string`, the runtime attempts to convert the `string` to a `number` before evaluating inequality.
5. **Boolean Coercion:** If one operand is a `boolean`, it is converted to a `number` (`true` becomes `1`, `false` becomes `0`) before comparison.
6. **Object to Primitive:** If one operand is an `object` and the other is a `string`, `number`, `bigint`, or `symbol`, the object is converted to a primitive value via its `valueOf()` or `toString()` methods before comparison.

## TypeScript Static Analysis Behavior

While TypeScript inherits the runtime behavior of `!=` from JavaScript, its static type checker restricts how the operator can be written. TypeScript prevents the comparison of mutually exclusive types, throwing a `TS2367` error, as such comparisons are logically flawed in a strongly typed environment.

```typescript theme={"dark"}
const str: string = "1";
const num: number = 1;

// TS2367: This condition will always return 'true' since the types 'string' and 'number' have no overlap.
const result = str != num; 
```

To observe the loose inequality coercion mechanics without triggering a compiler error, the operands must generally have overlapping types, or at least one operand must be typed as `any` or `unknown`.

However, the TypeScript compiler implements a specific, built-in exception for nullish values: it explicitly permits loose inequality comparisons between the distinct `null` and `undefined` types. This exception allows developers to check for both values simultaneously without throwing the `TS2367` (no overlap) error, even under strict type-checking.

```typescript theme={"dark"}
const looseString: any = "42";
const strictNumber: number = 42;

// Evaluates to false. "42" is coerced to 42, making them equal.
const isNotEqual = looseString != strictNumber; 

const nullVal: null = null;
const undefinedVal: undefined = undefined;

// Evaluates to false. Permitted by the compiler exception for nullish types.
const isNullishNotEqual = nullVal != undefinedVal; 
```

## Memory Reference Comparison

When both operands are objects (including arrays and functions), the `!=` operator does not compare their internal structures or values. Instead, it compares their memory references. It evaluates to `true` if the operands point to different locations in memory, regardless of whether their contents are identical.

```typescript theme={"dark"}
const objA: any = { id: 1 };
const objB: any = { id: 1 };
const objC: any = objA;

console.log(objA != objB); // true (different memory references)
console.log(objA != objC); // false (same memory reference)
```

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