Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Thanked 626 Times in 605 Posts
    Quote Originally Posted by felgall View Post
    I didn't say that JavaScript doesn't convert the string to a number when you do that comparison - it just might not be what you want ot to do for the comparison. My point is that there are multiple ways of converting from one type to another and the one JavaScript chooses when you don't specify it directly may not be the one you want.
    i'm not taking sides here, but i believe that the rules are not that complicated. I could probably teach it to my mom in an afternoon. It wouldn't be that hard because she's not entrenched into an older static way of conceiving operations while codifying desires. She's not entrenched in anything, as she's never programmed, but she can learn simple patterns.

    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.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  2. #17
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    Quote Originally Posted by rnd me View Post
    Anyone who lets a compiler or a browser choose anything for them is not exactly programming.
    When you use == != > or < without type casting at least one side of the comparison then you are letting the compiler or browser choose for you with regard to how the two will be compared if they are not the same type. Different languages make different choices.

    if ('04' < 4) // true in PHP, false in JavaScript
    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.
    Last edited by felgall; 01-08-2014 at 11:35 PM.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts