Code inspection: Using of coerced equality (using of coerced equality operator)
In JavaScript and TypeScript code, ReSharper suggests replacing the equality operator ==
with a stricter identity operator ===
. Using ===
is a good practice because ==
may work unpredictably — the reason is that the ==
operator performs type coercing (conversion) before comparison, which may lead to unexpected results of comparison. The identity operator ===
, which works only with operands of identical type, prevents such errors. You can learn more about these operators in the answers to this StackOverflow question.
In the example, ReSharper recommends using the identity operator:
function TestDisplay(id) {
if (document.getElementById(id).style.display == "") {
document.getElementById(id).style.display = "none";
}
//...
}
function TestDisplay(id) {
if (document.getElementById(id).style.display === "") {
document.getElementById(id).style.display = "none";
}
//...
}
Last modified: 08 April 2024