Js Comments in JavaScript
?? JavaScript Comparisons
Types of Comparison Operators
1. Equality
| Operator | Description | Example | Result |
|---|---|---|---|
== | Loose equality (value only) | 5 == '5' | true |
=== | Strict equality (value and type) | 5 === '5' | false |
2. Inequality
| Operator | Description | Example | Result |
|---|---|---|---|
!= | Loose inequality (value only) | 5 != '6' | true |
!== | Strict inequality (value or type) | 5 !== '5' | true |
3. Relational Operators
| Operator | Description | Example | Result |
|---|---|---|---|
< | Less than | 3 < 5 | true |
> | Greater than | 7 > 10 | false |
<= | Less than or equal | 5 <= 5 | true |
>= | Greater than or equal | 6 >= 4 | true |
Important Notes
Use strict equality (
===) to avoid unexpected type coercion.==converts operands to the same type before comparison — this can lead to surprises:
console.log(0 == false); // trueconsole.log(0 === false); // falseComparing Objects and Arrays
Objects and arrays are compared by reference, not by value.
const a = [1, 2];const b = [1, 2];console.log(a == b); // falseconsole.log(a === b); // falseconst c = a;console.log(a === c); // trueSpecial Cases
NaNis not equal to anything, including itself:
console.log(NaN == NaN); // falseconsole.log(NaN === NaN); // falseUse
Number.isNaN()to check forNaN.
Want me to explain type coercion rules or give examples of common pitfalls with comparisons?