Code Inspection: Using of coerced equality
In JavaScript and TypeScript code, JetBrains Rider 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, JetBrains Rider 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: 21 March 2024