JavaScript provides different operators for comparing values. These operators are used to compare two values and return a boolean (true
or false
).
1. Equality (==
) and Strict Equality (===
)
- Equality (
==
): Compares two values for equality after converting both to a common type (type coercion). - Strict Equality (
===
): Compares two values for equality without converting their types.console.log(5 == '5'); // true (type coercion) console.log(5 === '5'); // false (no type coercion)
2. Inequality (!=
) and Strict Inequality (!==
)
- Inequality (
!=
): Compares two values for inequality after converting both to a common type. - Strict Inequality (
!==
): Compares two values for inequality without converting their types.console.log(5 != '5'); // false (type coercion) console.log(5 !== '5'); // true (no type coercion)
3. Greater Than (>
) and Less Than (<
)
- Greater Than (
>
): Checks if the left operand is greater than the right operand. - Less Than (
<
): Checks if the left operand is less than the right operand.console.log(7 > 5); // true console.log(3 < 5); // true
4. Greater Than or Equal To (>=
) and Less Than or Equal To (<=
)
- Greater Than or Equal To (
>=
): Checks if the left operand is greater than or equal to the right operand. - Less Than or Equal To (
<=
): Checks if the left operand is less than or equal to the right operand.console.log(5 >= 5); // true console.log(5 <= 7); // true
5. Comparing Different Types
JavaScript will convert values to a common type when using non-strict comparison operators (==
, !=
). This can lead to unexpected results. It’s generally recommended to use strict comparison (===
, !==
) to avoid type coercion.
console.log('5' == 5); // true (string is converted to number) console.log('5' === 5); // false (different types)
6. Special Cases in Comparisons
NaN
is Not Equal to Itself: UseisNaN()
to check if a value isNaN
.console.log(NaN == NaN); // false console.log(isNaN(NaN)); // true
null
andundefined
Comparisons:null
andundefined
are equal to each other but not to other values.console.log(null == undefined); // true console.log(null === undefined); // false
7. Objects and Arrays Comparisons
- Objects and arrays are compared by reference, not by value.
const obj1 = { a: 1 }; const obj2 = { a: 1 }; console.log(obj1 == obj2); // false console.log(obj1 === obj2); // false
- Comparing the same reference returns
true
.const obj1 = { a: 1 }; const obj2 = obj1; console.log(obj1 == obj2); // true console.log(obj1 === obj2); // true
8. Boolean Comparisons
true
is converted to1
andfalse
to0
in non-strict comparisons.console.log(true == 1); // true console.log(false == 0); // true
Summary
- Use strict equality (
===
) for most comparisons to avoid unexpected results from type coercion. - Be cautious when comparing objects, arrays, and functions, as they are compared by reference.
- Understand special cases like
NaN
andnull
/undefined
to prevent bugs.
Properly understanding and using comparisons in JavaScript is essential for writing reliable and bug-free code.