Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 16 to 17 of 17
Thread: Issues with logical operators
01-08-2014, 09:09 PM #16
- Join Date
- Jun 2007
- Thanked 613 Times in 593 Posts
Anyone who lets a compiler or a browser choose anything for them is not exactly programming. Ignorance of the law is no excuse. Never say never about anything, even the confusing parts of JS have their utility in the right hands.
01-08-2014, 10:23 PM #17
- Join Date
- Sep 2005
- Sydney, Australia
- Thanked 693 Times in 682 Posts
if(Number('04') < 4)// false in both languages
If they are supposed to be the same type then there's no need to use == or != as === and !== work there just as well without opening up the possibility of problems if it turns out they are not the same type. I think this is the only point where Philip and I are actually disagreeing.
Perhaps the way to word it should be :
Use === and !== where ever possible. When you are forced to do comparisons using == != < and > always make sure that at least one side of the comparison is forced to a given type before doing the compare so that you and not the compiler/browser decides which way the conversion will be done if the two turn out to be of different types.
var a = 3, b = 4;
if (+a == a && a < +b)
ensures that both are numbers before the comparison whereas just (a < b) may incorrectly return true if one of both is not a number.