Pattern: Variable compared to itself
Issue: -
Comparing a variable with itself is usually a mistake, either a typo or refactoring error. Such comparisons are either always true for equality or always false for inequality, making them logically meaningless.
Example of incorrect code:
var x = 10;
if (x === x) {
doSomething();
}
if (obj.prop !== obj.prop) {
handle();
}
if (arr[0] >= arr[0]) {
process();
}
Example of correct code:
var x = 10;
var y = 20;
if (x === y) {
doSomething();
}
if (obj.prop !== obj.otherProp) {
handle();
}
if (arr[0] >= arr[1]) {
process();
}