Loading ...
Sorry, an error occurred while loading the content.

option.eqeq

Expand Messages
  • Douglas Crockford
    There is now a Tolerate == and != option. It is not recommended.
    Message 1 of 13 , Jun 12, 2011
    • 0 Attachment
      There is now a Tolerate == and != option. It is not recommended.
    • Sean Soria
      Is there any reason to avoid using == and != to check for both null or undefined? I have read that x == null will only return true when x is either null or
      Message 2 of 13 , Jun 14, 2011
      • 0 Attachment
        Is there any reason to avoid using == and != to check for both null or
        undefined? I have read that x == null will only return true when x is
        either null or undefined.

        On Sun, Jun 12, 2011 at 3:06 AM, Douglas Crockford
        <douglas@...> wrote:
        > There is now a Tolerate == and != option. It is not recommended.
        >
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
      • Douglas Crockford
        ... Yes, there is a good reason to not use == at all. If you are smart, you will not use option.eqeq.
        Message 3 of 13 , Jun 15, 2011
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, Sean Soria <sean.soria@...> wrote:
          >
          > Is there any reason to avoid using == and != to check for both null or
          > undefined? I have read that x == null will only return true when x is
          > either null or undefined.


          Yes, there is a good reason to not use == at all. If you are smart, you will not use option.eqeq.
        • Sean Soria
          i understand type coersion and why == is bad in general, but it seems to me that when testing for null or undefined if (x == null) is more readable than if
          Message 4 of 13 , Jun 15, 2011
          • 0 Attachment
            i understand type coersion and why == is bad in general, but it seems
            to me that when testing for null or undefined "if (x == null)" is more
            readable than "if (x === null || x === undefined)" and safer than "if
            (x)".

            On Wed, Jun 15, 2011 at 5:35 AM, Douglas Crockford
            <douglas@...> wrote:
            > --- In jslint_com@yahoogroups.com, Sean Soria <sean.soria@...> wrote:
            >>
            >> Is there any reason to avoid using == and != to check for both null or
            >> undefined?  I have read that x == null will only return true when x is
            >> either null or undefined.
            >
            >
            > Yes, there is a good reason to not use == at all. If you are smart, you will not use option.eqeq.
            >
            >
            >
            > ------------------------------------
            >
            > Yahoo! Groups Links
            >
            >
            >
            >
          • Jakob Kruse
            Sean, Using x == null may seem more readable, but actually almost all of the JavaScript developers I have met are unsure what that means exactly. Or rather,
            Message 5 of 13 , Jun 15, 2011
            • 0 Attachment
              Sean,


              Using "x == null" may seem more readable, but actually almost all of the JavaScript developers I have met are unsure what that means exactly. Or rather, many of them are sure they know exactly what it means, but they are incorrect.


              Spelling it out with === is highly recommended.


              /Jakob

              _____

              From: Sean Soria [mailto:sean.soria@...]
              To: jslint_com@yahoogroups.com
              Sent: Wed, 15 Jun 2011 15:40:18 +0200
              Subject: Re: [jslint] option.eqeq






              i understand type coersion and why == is bad in general, but it seems
              to me that when testing for null or undefined "if (x == null)" is more
              readable than "if (x === null || x === undefined)" and safer than "if
              (x)".

              On Wed, Jun 15, 2011 at 5:35 AM, Douglas Crockford
              <douglas@...> wrote:
              > --- In jslint_com@yahoogroups.com, Sean Soria <sean.soria@...> wrote:
              >>
              >> Is there any reason to avoid using == and != to check for both null or
              >> undefined? I have read that x == null will only return true when x is
              >> either null or undefined.
              >
              >
              > Yes, there is a good reason to not use == at all. If you are smart, you will not use option.eqeq.
              >
              >
              >
              > ------------------------------------
              >
              > Yahoo! Groups Links
              >
              >
              >
              >



              [Non-text portions of this message have been removed]
            • Luke Page
              and even if you do know what it means, using === for every thing and then using == for null could easily lead to misreading the == null when it appears. I do
              Message 6 of 13 , Jun 15, 2011
              • 0 Attachment
                and even if you do know what it means, using === for every thing and then
                using == for null could easily lead to misreading the == null when it
                appears.

                I do not like if (a === null || a === undefined) so I try to avoid it
                alltogether and so if I can assume that a variable will be populated with a
                object, not a string or number then if(a) would be my preferred notation.
                This way I find it rare to have to test for null and undefined.

                On 15 June 2011 14:47, Jakob Kruse <kruse@...> wrote:

                >
                >
                > Sean,
                >
                > Using "x == null" may seem more readable, but actually almost all of the
                > JavaScript developers I have met are unsure what that means exactly. Or
                > rather, many of them are sure they know exactly what it means, but they are
                > incorrect.
                >
                > Spelling it out with === is highly recommended.
                >
                > /Jakob
                >
                > _____
                >
                > From: Sean Soria [mailto:sean.soria@...]
                > To: jslint_com@yahoogroups.com
                > Sent: Wed, 15 Jun 2011 15:40:18 +0200
                > Subject: Re: [jslint] option.eqeq
                >
                >
                > i understand type coersion and why == is bad in general, but it seems
                > to me that when testing for null or undefined "if (x == null)" is more
                > readable than "if (x === null || x === undefined)" and safer than "if
                > (x)".
                >
                > On Wed, Jun 15, 2011 at 5:35 AM, Douglas Crockford
                > <douglas@...> wrote:
                > > --- In jslint_com@yahoogroups.com, Sean Soria <sean.soria@...> wrote:
                > >>
                > >> Is there any reason to avoid using == and != to check for both null or
                > >> undefined? I have read that x == null will only return true when x is
                > >> either null or undefined.
                > >
                > >
                > > Yes, there is a good reason to not use == at all. If you are smart, you
                > will not use option.eqeq.
                > >
                > >
                > >
                > > ------------------------------------
                > >
                > > Yahoo! Groups Links
                > >
                > >
                > >
                > >
                >
                >
                >
                > [Non-text portions of this message have been removed]
                >
                >
                >


                [Non-text portions of this message have been removed]
              • Robert Sauer-Ernst
                ... Von: Jakob Kruse Am: Wed, 15 Jun 2011 15:47:46 +0200 Betreff: Re: [jslint] option.eqeq An: jslint_com@yahoogroups.com ... why does it
                Message 7 of 13 , Jun 15, 2011
                • 0 Attachment
                  --- Original Nachricht ---

                  Von: Jakob Kruse <kruse@...>
                  Am: Wed, 15 Jun 2011 15:47:46 +0200
                  Betreff: Re: [jslint] option.eqeq
                  An: jslint_com@yahoogroups.com
                  CC:
                  >
                  > Sean,
                  >
                  > Using "x == null" may seem more readable,
                  >

                  why does it seem more readable? it is a shortcut. when i read it loud
                  its "check, if x value is, not considering its type, equal to null, not
                  considering its type". instead of: "check wether its excatly null or
                  undefined". readability might be a matter of taste. the point you made,
                  that many people misunderstand this statement, should not be one.
                • Jakob Kruse
                  I should clarify that I do *not* find it more readable. What I said was it might *seem* more readable. The truth is it s almost impossible to figure out
                  Message 8 of 13 , Jun 15, 2011
                  • 0 Attachment
                    I should clarify that I do *not* find it more readable. What I said was it might *seem* more readable. The truth is it's almost impossible to figure out exactly what it means.


                    /Jakob



                    _____

                    From: Robert Sauer-Ernst [mailto:facebook@...]
                    To: jslint_com@yahoogroups.com
                    Sent: Wed, 15 Jun 2011 15:57:48 +0200
                    Subject: Re: [jslint] option.eqeq







                    --- Original Nachricht ---

                    Von: Jakob Kruse <kruse@...>
                    Am: Wed, 15 Jun 2011 15:47:46 +0200
                    Betreff: Re: [jslint] option.eqeq
                    An: jslint_com@yahoogroups.com
                    CC:
                    >
                    > Sean,
                    >
                    > Using "x == null" may seem more readable,
                    >

                    why does it seem more readable? it is a shortcut. when i read it loud
                    its "check, if x value is, not considering its type, equal to null, not
                    considering its type". instead of: "check wether its excatly null or
                    undefined". readability might be a matter of taste. the point you made,
                    that many people misunderstand this statement, should not be one.



                    [Non-text portions of this message have been removed]
                  • Michael S. Mikowski
                    If you are testing falsey or truthy values, I have found this is a nice way to do it tersely without having jslint complain: if ( ! obj_a ){ // do stuff
                    Message 9 of 13 , Jun 15, 2011
                    • 0 Attachment
                      If you are testing "falsey" or "truthy" values, I have found this is a nice
                      way to do it tersely without having jslint complain:

                      if ( ! obj_a ){ // do stuff here
                      }


                      On Wednesday, June 15, 2011 07:04:35 AM Jakob Kruse wrote:
                      > I should clarify that I do *not* find it more readable. What I said was it
                      > might *seem* more readable. The truth is it's almost impossible to figure
                      > out exactly what it means.
                      >
                      >
                      > /Jakob
                      >
                      >
                      >
                      > _____
                      >
                      > From: Robert Sauer-Ernst [mailto:facebook@...]
                      > To: jslint_com@yahoogroups.com
                      > Sent: Wed, 15 Jun 2011 15:57:48 +0200
                      > Subject: Re: [jslint] option.eqeq
                      >
                      >
                      >
                      >
                      >
                      >
                      >
                      > --- Original Nachricht ---
                      >
                      > Von: Jakob Kruse <kruse@...>
                      > Am: Wed, 15 Jun 2011 15:47:46 +0200
                      > Betreff: Re: [jslint] option.eqeq
                      > An: jslint_com@yahoogroups.com
                      >
                      > CC:
                      > > Sean,
                      > >
                      > > Using "x == null" may seem more readable,
                      >
                      > why does it seem more readable? it is a shortcut. when i read it loud
                      > its "check, if x value is, not considering its type, equal to null, not
                      > considering its type". instead of: "check wether its excatly null or
                      > undefined". readability might be a matter of taste. the point you made,
                      > that many people misunderstand this statement, should not be one.
                      >
                      >
                      >
                      > [Non-text portions of this message have been removed]


                      [Non-text portions of this message have been removed]
                    • livefree75
                      ... I have found only one instance in all my code where == is useful. There are times when I want to see if a given value is in an array, but I don t care (or
                      Message 10 of 13 , Jun 16, 2011
                      • 0 Attachment
                        --- In jslint_com@yahoogroups.com, Sean Soria <sean.soria@...> wrote:
                        >
                        > Is there any reason to avoid using == and != to check for both null or
                        > undefined? I have read that x == null will only return true when x is
                        > either null or undefined.
                        >

                        I have found only one instance in all my code where == is useful.

                        There are times when I want to see if a given value is in an array, but
                        I don't care (or want to worry about) what its type is... e.g. the value
                        of a Select object, which is a defined as a string, being compared
                        against an array of integers.

                        I created a function that checks whether a given value is in an array,
                        and you can specify whether to care about type. Something like this:

                        /**
                        * Checks the existence of an item in an array.
                        * @param Array arr The array to search
                        * @param mixed item The item to search for
                        * @param Boolean strict If false, does not compare types
                        * (default is true.)
                        */
                        function inArray(arr, item, strict) {
                        var i=0;
                        if (compare_types !== false) {
                        for (; i<arr.length; i++) {
                        if (arr[i] === item) {
                        return true;
                        }
                        }
                        } else { // strict === false
                        for (; i<arr.length; i++) {
                        if (arr[i] == item) {
                        return true;
                        }
                        }
                        }
                        return false;
                        }

                        This fails JSLint because of the == in the second for loop. However,
                        checking for Object equality, then doing .toString() seems like much
                        more work than just using ==.

                        Jamie



                        [Non-text portions of this message have been removed]
                      • Douglas Crockford
                        ... I found zero useful instances of == in your code. Your use of == resulted in two inefficiencies and one error. The first if is looking at compare_types,
                        Message 11 of 13 , Jun 16, 2011
                        • 0 Attachment
                          --- In jslint_com@yahoogroups.com, "livefree75" <jpittman2@...> wrote:
                          >
                          > --- In jslint_com@yahoogroups.com, Sean Soria <sean.soria@> wrote:
                          > >
                          > > Is there any reason to avoid using == and != to check for both null or
                          > > undefined? I have read that x == null will only return true when x is
                          > > either null or undefined.
                          > >
                          >
                          > I have found only one instance in all my code where == is useful.
                          >
                          > There are times when I want to see if a given value is in an array, but
                          > I don't care (or want to worry about) what its type is... e.g. the value
                          > of a Select object, which is a defined as a string, being compared
                          > against an array of integers.
                          >
                          > I created a function that checks whether a given value is in an array,
                          > and you can specify whether to care about type. Something like this:
                          >
                          > /**
                          > * Checks the existence of an item in an array.
                          > * @param Array arr The array to search
                          > * @param mixed item The item to search for
                          > * @param Boolean strict If false, does not compare types
                          > * (default is true.)
                          > */
                          > function inArray(arr, item, strict) {
                          > var i=0;
                          > if (compare_types !== false) {
                          > for (; i<arr.length; i++) {
                          > if (arr[i] === item) {
                          > return true;
                          > }
                          > }
                          > } else { // strict === false
                          > for (; i<arr.length; i++) {
                          > if (arr[i] == item) {
                          > return true;
                          > }
                          > }
                          > }
                          > return false;
                          > }
                          >
                          > This fails JSLint because of the == in the second for loop. However,
                          > checking for Object equality, then doing .toString() seems like much
                          > more work than just using ==.


                          I found zero useful instances of == in your code. Your use of == resulted in two inefficiencies and one error. The first if is looking at compare_types, but it probably should be looking at strict instead.

                          I think the use of strict should be

                          if (!strict) {
                          item = +item;
                          }

                          That way JS does not have to do type coercion on every iteration.
                          Even better, you do not need the extra == loop.

                          A further improvement would be to use array.indexOf instead.

                          JSLint's advice was good. You should have taken advantage of it.
                        • Luke Page
                          I m a strong supporter in having the option available BUT only for use in legacy code that is not yet jslint compliant. So please keep the option. And please
                          Message 12 of 13 , Jun 16, 2011
                          • 0 Attachment
                            I'm a strong supporter in having the option available BUT only for use in
                            legacy code that is not yet jslint compliant.

                            So please keep the option. And please don't use it!
                            On 16 Jun 2011 19:50, "Douglas Crockford" <douglas@...> wrote:
                            > --- In jslint_com@yahoogroups.com, "livefree75" <jpittman2@...> wrote:
                            >>
                            >> --- In jslint_com@yahoogroups.com, Sean Soria <sean.soria@> wrote:
                            >> >
                            >> > Is there any reason to avoid using == and != to check for both null or
                            >> > undefined? I have read that x == null will only return true when x is
                            >> > either null or undefined.
                            >> >
                            >>
                            >> I have found only one instance in all my code where == is useful.
                            >>
                            >> There are times when I want to see if a given value is in an array, but
                            >> I don't care (or want to worry about) what its type is... e.g. the value
                            >> of a Select object, which is a defined as a string, being compared
                            >> against an array of integers.
                            >>
                            >> I created a function that checks whether a given value is in an array,
                            >> and you can specify whether to care about type. Something like this:
                            >>
                            >> /**
                            >> * Checks the existence of an item in an array.
                            >> * @param Array arr The array to search
                            >> * @param mixed item The item to search for
                            >> * @param Boolean strict If false, does not compare types
                            >> * (default is true.)
                            >> */
                            >> function inArray(arr, item, strict) {
                            >> var i=0;
                            >> if (compare_types !== false) {
                            >> for (; i<arr.length; i++) {
                            >> if (arr[i] === item) {
                            >> return true;
                            >> }
                            >> }
                            >> } else { // strict === false
                            >> for (; i<arr.length; i++) {
                            >> if (arr[i] == item) {
                            >> return true;
                            >> }
                            >> }
                            >> }
                            >> return false;
                            >> }
                            >>
                            >> This fails JSLint because of the == in the second for loop. However,
                            >> checking for Object equality, then doing .toString() seems like much
                            >> more work than just using ==.
                            >
                            >
                            > I found zero useful instances of == in your code. Your use of == resulted
                            in two inefficiencies and one error. The first if is looking at
                            compare_types, but it probably should be looking at strict instead.
                            >
                            > I think the use of strict should be
                            >
                            > if (!strict) {
                            > item = +item;
                            > }
                            >
                            > That way JS does not have to do type coercion on every iteration.
                            > Even better, you do not need the extra == loop.
                            >
                            > A further improvement would be to use array.indexOf instead.
                            >
                            > JSLint's advice was good. You should have taken advantage of it.
                            >
                            >


                            [Non-text portions of this message have been removed]
                          • livefree75
                            ... You re right of course on the compare_types vs. strict - I just typed that up quickly in reply to the message, and changed the variable name in the
                            Message 13 of 13 , Jun 17, 2011
                            • 0 Attachment
                              --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
                              >
                              > --- In jslint_com@yahoogroups.com, "livefree75" <jpittman2@> wrote:
                              > >
                              > > I have found only one instance in all my code where == is useful.
                              > >
                              > > There are times when I want to see if a given value is in an array, but
                              > > I don't care (or want to worry about) what its type is... e.g. the value
                              > > of a Select object, which is a defined as a string, being compared
                              > > against an array of integers.
                              > >
                              > > I created a function that checks whether a given value is in an array,
                              > > and you can specify whether to care about type. Something like this:
                              > >
                              > > /**
                              > > * Checks the existence of an item in an array.
                              > > * @param Array arr The array to search
                              > > * @param mixed item The item to search for
                              > > * @param Boolean strict If false, does not compare types
                              > > * (default is true.)
                              > > */
                              > > function inArray(arr, item, strict) {
                              > > var i=0;
                              > > if (compare_types !== false) {
                              > > for (; i<arr.length; i++) {
                              > > if (arr[i] === item) {
                              > > return true;
                              > > }
                              > > }
                              > > } else { // strict === false
                              > > for (; i<arr.length; i++) {
                              > > if (arr[i] == item) {
                              > > return true;
                              > > }
                              > > }
                              > > }
                              > > return false;
                              > > }
                              > >
                              > > This fails JSLint because of the == in the second for loop. However,
                              > > checking for Object equality, then doing .toString() seems like much
                              > > more work than just using ==.
                              >
                              >
                              > I found zero useful instances of == in your code. Your use of == resulted in two inefficiencies and one error. The first if is looking at compare_types, but it probably should be looking at strict instead.
                              >
                              > I think the use of strict should be
                              >
                              > if (!strict) {
                              > item = +item;
                              > }

                              You're right of course on the compare_types vs. strict - I just typed that up quickly in reply to the message, and changed the variable name in the definition but forgot to do it in the function body.

                              item = +item would coerce item into a number, so my function would be useless for strings, as all non-numeric strings would be coerced into NaN.

                              Am I missing something?

                              >
                              > That way JS does not have to do type coercion on every iteration.
                              > Even better, you do not need the extra == loop.
                              >
                              > A further improvement would be to use array.indexOf instead.

                              OK - now that I'm looking at my actual code - I wasn't doing it for array - I was doing it for Objects, and it's in the context of jQuery:

                              (function($) {
                              $.extend({
                              inObject : function(value, object, strict) {
                              var found_key = null;
                              if (strict !== false) {
                              $.each(object, function(key, val) {
                              if (val===value) {
                              found_key = key;
                              return false;
                              }
                              });
                              } else {
                              $.each(object, function(key, val) {
                              if (val==value) {
                              found_key = key;
                              return false;
                              }
                              });
                              }
                              return found_key;
                              } // inObject()
                              });
                              }(jQuery));


                              >
                              > JSLint's advice was good. You should have taken advantage of it.
                              >

                              Well that's the point of forums like this, right?

                              BTW - your forin directive doesn't flag $.each() constructs.

                              Also, before I used $.each(), I used to do this (this was also before I knew about the Object.hasOwnProperty()):

                              for ( i in obj ) {
                              if (typeof obj[i] !== 'function') {
                              // body of my for-in loop
                              }
                              }

                              But I would want to do this, to avoid the busywork of indenting.
                              for ( i in obj ) {
                              if (typeof obj[i] === 'function') { continue; }
                              // body of my for-in loop
                              }

                              But JSLint didn't accept that as proper filtering, which of course it is. I would assume the same is true for Object.hasOwnProperty().

                              Jamie
                            Your message has been successfully submitted and would be delivered to recipients shortly.