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 loose inequality operator (!=) evaluates whether two operands are not equal, returning a boolean result. Unlike the strict inequality operator (!==), it performs automatic type coercion via the Abstract Equality Comparison algorithm if the operands are of different data types before making the comparison. It is the exact logical negation of the loose equality operator (==).
operand1 != operand2

Evaluation Mechanics

When evaluating a != b, the JavaScript engine applies the following rules based on the types of the operands:
  1. Identical Types: If both operands are of the same type, no coercion occurs. The operator compares their values directly.
  2. NaN: If either or both operands are NaN, the operator always returns true. By specification, NaN is never equal to any value, including itself.
  3. Null and Undefined: null and undefined are considered loosely equal to each other. Therefore, null != undefined evaluates to false. They are not loosely equal to any other value.
  4. Number, String, and BigInt: If one operand is a String and the other is a Number or BigInt, the engine attempts to parse the String into a numeric value before comparing. If comparing a Number and a BigInt directly, they are compared by their mathematical value without strict type matching.
  5. Booleans: If either operand is a Boolean, it is coerced into a Number (true becomes 1, false becomes 0) before comparison.
  6. 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 value using its internal [Symbol.toPrimitive](), valueOf(), or toString() methods before the comparison proceeds.

Syntax and Coercion Visualization

// 1. Identical Types & NaN
10 != 20;                // true
"test" != "test";        // false
NaN != NaN;              // true  (NaN is never equal to anything)

// 2. Null and Undefined
null != undefined;       // false (They are loosely equal)
null != 0;               // true  (Null does not coerce to 0 in equality checks)

// 3. Number, String, and BigInt Coercion
5 != "5";                // false ("5" is coerced to Number 5)
1n != 1;                 // false (Compared by mathematical value)
10n != "10";             // false ("10" is coerced to BigInt 10n)
0 != "";                 // false ("" is coerced to Number 0)

// 4. Boolean Coercion
0 != false;              // false (false is coerced to 0)
1 != true;               // false (true is coerced to 1)
2 != true;               // true  (true is coerced to 1; 2 != 1)

// 5. Object to Primitive Coercion
[1] != 1;                // false ([1] toString() becomes "1", then Number 1)
[""] != 0;               // false ([""] toString() becomes "", then Number 0)
[1, 2] != "1,2";         // false ([1, 2] toString() becomes "1,2")

Memory Reference Comparison

When both operands are Objects (including Arrays and Functions), the != operator does not compare their structural content or properties. Instead, it compares their references in memory. It will only return false if both operands point to the exact same object instance.
const objA = { id: 1 };
const objB = { id: 1 };
const objC = objA;

objA != objB; // true  (Different memory references)
objA != objC; // false (Same memory reference)
Master JavaScript with Deep Grasping Methodology!Learn More