Comparison operators compare values

Software ResiliencyProgramming Best Practices

Why you should care

This is important since Javascript has comparison operators such as “regular equality” (==) and “is not equal to” (!=) which compares two values (strings, numbers, boolean operands and so forth) for equality. With “regular equality”, two values are set to identical after being converted to a common type.
“Is not equal to” is utilized to ensure that the values are not identical to each other. Without a comparison operator, values cannot be compared.

Business Impacts

It is recommended to avoid implied typecasting as it causes confusion on the technical side. This results in allowing more risks in the code and leading to lack of productivity.

Production Risk

How we detect

This code insight show that JavaScript implicitly typecasts variables when you compare them. That’s why comparisons such as false == 0 or “” == 0 return true.
To avoid confusion caused by the implied typecasting, always use the === and !== operators that check both the values and the type of the expressions you compare, because the understanding of what is a falsy value may be confusing.
The == and != operators do type coercion. In particular, do not use == to compare against false values.

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.

See featuresHow it works