Comparison may cause unexpected type coercion...

This inspection warning pops up on any JavaScript using == or != apparently:

This seems unnecessary as I've had to fix code where === or !== was used everywhere (as "good practice" supposedly) because the code would fail when it encountered an unexpected datatype even though all that mattered was the value...

For instance, say you get in XML or JSON from a request response, and your if statement is `if(value === 86)` but the XML/JSON value is string '86', the processing will fail even though the value matched and that's all that was wanted from the response. For the === to work properly, you'd either have to type '86' or convert the value to an integer... which is far more coding than simply deleting an =. 

In some case I've seen datatype convert through various processes or functions or even languages, and while the value remains the same, it will not be seen as the same by this "good practice". It's as good as being completely different in this case.

I want to disable this inspection because it is highlighting EVERY value check in my JavaScript, but I'm not sure if it will ever be useful, I just think it is a very overboard warning and not a good idea to be recommending datatype check on EVERYTHING, especially when PHP handles type conversion so well (this is PHPStorm after all and we're also using PHP code) and JavaScript is close. 


Hopefully my original post will not be misread.


Not sure I follow you... Indeed, if you don't care about using explicit types conversion, and intentionally rely on javascript type coercion, comparing string to integer, changing `==` to `===` would result in unexpected result.

The IDE does not force you to use this or that coding style. You can disable this inspection completely, or suppress it for the statements where type coercion is used intentionally


I'm saying the warning is overly broad, and its advice is probably not good practice in PHP with its auto-conversion, as usually the value matters but not the datatype, and checking datatype when it's not necessary can cause failure in real-world scenarios.


Please sign in to leave a comment.