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

Re: indexOf in an If clause

Expand Messages
  • Merlin
    ... lastIndexOf() has the same issue.
    Message 1 of 7 , Apr 24 10:02 AM
    View Source
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
      >
      > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@> wrote:
      >
      > > So for my book, having a indexOf in an if condition without a comparison
      > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
      > > not paying attention.
      >
      > Please try it now.
      >
      > Are there any other standard functions with similar hazards?
      >

      lastIndexOf() has the same issue.
    • Merlin
      ... search(regexp) may have the same issue.
      Message 2 of 7 , Apr 24 10:08 AM
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
        >
        > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@> wrote:
        >
        > > So for my book, having a indexOf in an if condition without a comparison
        > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
        > > not paying attention.
        >
        > Please try it now.
        >
        > Are there any other standard functions with similar hazards?
        >

        search(regexp) may have the same issue.
      • Jordan
        ... `if (!a.indexOf( c ))` and `if (!!a.indexOf( c ))` both do not trigger the weird condition error. Is it worth also checking that Array.length is always
        Message 3 of 7 , Apr 24 10:31 AM
        View Source
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@> wrote:
          >
          > > So for my book, having a indexOf in an if condition without a comparison
          > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
          > > not paying attention.
          >
          > Please try it now.
          >
          > Are there any other standard functions with similar hazards?
          >

          `if (!a.indexOf('c'))` and `if (!!a.indexOf('c'))` both do not trigger the "weird condition" error. Is it worth also checking that Array.length is always used in an integer context and not a boolean one?
        • Simon Kenyon Shepard
          works great. Thanks. ... -- We are the music makers, And we are the dreamers of dreams [Non-text portions of this message have been removed]
          Message 4 of 7 , Apr 24 3:03 PM
          View Source
          • 0 Attachment
            works great. Thanks.

            On 24 April 2011 18:52, Douglas Crockford <douglas@...> wrote:

            >
            >
            > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@...>
            > wrote:
            >
            > > So for my book, having a indexOf in an if condition without a comparison
            > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
            > > not paying attention.
            >
            > Please try it now.
            >
            > Are there any other standard functions with similar hazards?
            >
            >
            >



            --
            "We are the music makers, And we are the dreamers of dreams"


            [Non-text portions of this message have been removed]
          • Douglas Crockford
            ... Yes, I think they should. ... No, I don t think so. The confusion with indexOf is that the not-found value is truthy, and only one of the found values is
            Message 5 of 7 , Apr 24 6:02 PM
            View Source
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "Jordan" <ljharb@...> wrote:
              > `if (!a.indexOf('c'))` and `if (!!a.indexOf('c'))` both do not trigger the "weird condition" error.

              Yes, I think they should.

              > Is it worth also checking that Array.length is always used in an integer context and not a boolean one?

              No, I don't think so. The confusion with indexOf is that the not-found value is truthy, and only one of the found values is falsy. length does not have that confusion.
            Your message has been successfully submitted and would be delivered to recipients shortly.