TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
!= (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.
Evaluation Mechanics
When evaluatingoperand1 != operand2, the JavaScript runtime underlying TypeScript applies the Abstract Equality Comparison algorithm. The mechanics are as follows:
- Identical Types: If both operands are of the same type, no coercion occurs. The operator behaves identically to
!==. - NaN Behavior: The
NaN(Not-a-Number) value is never equal to any value, including itself. Therefore,NaN != NaNalways evaluates totrue. - Nullish Operands:
nullandundefinedare considered loosely equal to each other, but not to any other values. Therefore,null != undefinedevaluates tofalse. Comparing any object tonullorundefinedevaluates totruewithout triggering any type conversion. - Primitive Coercion: If comparing a
numberand astring, the runtime attempts to convert thestringto anumberbefore evaluating inequality. - Boolean Coercion: If one operand is a
boolean, it is converted to anumber(truebecomes1,falsebecomes0) before comparison. - Object to Primitive: If one operand is an
objectand the other is astring,number,bigint, orsymbol, the object is converted to a primitive value via itsvalueOf()ortoString()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.
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.
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.
Master TypeScript with Deep Grasping Methodology!Learn More





