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

Re: [jslint] Why isn't !! OK?

Expand Messages
  • Julien Royer
    ... It doesn t emit this error for the following code: /*global b*/ var a = !!b; Do you have a code snippet illustrating your problem?
    Message 1 of 6 , Feb 1, 2010
    • 0 Attachment
      > !! can be used as a prefix operator, converting its operand to a boolean.
      >
      > but JSLint says:
      >
      > Confusing use of '!'.

      It doesn't emit this error for the following code:

      /*global b*/
      var a = !!b;

      Do you have a code snippet illustrating your problem?
    • iain_dalton
      ... Sure: var launchIsKeyCode = this.prefs.getBoolPref(this.kbLaunchNames.userIsKeyCode); var charCode = event.charCode; if ((!!charCode) === launchIsKeyCode)
      Message 2 of 6 , Feb 1, 2010
      • 0 Attachment
        > It doesn't emit this error for the following code:
        >
        > /*global b*/
        > var a = !!b;
        >
        > Do you have a code snippet illustrating your problem?

        Sure:

        var launchIsKeyCode = this.prefs.getBoolPref(this.kbLaunchNames.userIsKeyCode);
        var charCode = event.charCode;
        if ((!!charCode) === launchIsKeyCode) {
        return;
        }
      • Douglas Crockford
        ... I have to confess, this code confuses me. What is it intending to do? Could it be made clearer?
        Message 3 of 6 , Feb 1, 2010
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "iain_dalton" <iain.dalton@...> wrote:

          > var launchIsKeyCode = this.prefs.getBoolPref(this.kbLaunchNames.userIsKeyCode);
          > var charCode = event.charCode;
          > if ((!!charCode) === launchIsKeyCode) {
          > return;
          > }

          I have to confess, this code confuses me.
          What is it intending to do? Could it be made clearer?
        • Michael Lorton
          It seems pellucid to me. It means is the truthiness of charCode the same as the actual truth-value of launchIsKeyCode . In fact, I cannot think of a better
          Message 4 of 6 , Feb 1, 2010
          • 0 Attachment
            It seems pellucid to me. It means "is the truthiness of charCode the same as the actual truth-value of launchIsKeyCode". In fact, I cannot think of a better way to express it. The only obvious variation is

            if (!charCode === !launchIsKeyCode)

            and that isn't exactly equivalent, since it would give a different answer if charCode were truthy and launchIsKeyCode were truthy but not a boolean true.

            M.



            ________________________________
            From: Douglas Crockford <douglas@...>
            To: jslint_com@yahoogroups.com
            Sent: Tue, February 2, 2010 5:44:17 AM
            Subject: Re: [jslint] Why isn't !! OK?

            --- In jslint_com@yahoogroups.com, "iain_dalton" <iain.dalton@...> wrote:

            > var launchIsKeyCode = this.prefs.getBoolPref(this.kbLaunchNames.userIsKeyCode);
            > var charCode = event.charCode;
            > if ((!!charCode) === launchIsKeyCode) {
            > return;
            > }

            I have to confess, this code confuses me.
            What is it intending to do? Could it be made clearer?



            ------------------------------------

            Yahoo! Groups Links



            [Non-text portions of this message have been removed]
          • Jordan
            I don t understand why the logical test can t be: if ((event.charCode && launchIsKeyCode) || (!event.charCode && !launchIsKeyCode)) { You don t need to force a
            Message 5 of 6 , Feb 2, 2010
            • 0 Attachment
              I don't understand why the logical test can't be:

              if ((event.charCode && launchIsKeyCode) || (!event.charCode && !launchIsKeyCode)) {

              You don't need to force a boolean when doing a logical comparison test, and what you want is the opposite of xor - when both are true or both are false. For readability's sake, why not just state both conditions?

              - Jordan

              --- In jslint_com@yahoogroups.com, Michael Lorton <mlorton@...> wrote:
              >
              > It seems pellucid to me. It means "is the truthiness of charCode the same as the actual truth-value of launchIsKeyCode". In fact, I cannot think of a better way to express it. The only obvious variation is
              >
              > if (!charCode === !launchIsKeyCode)
              >
              > and that isn't exactly equivalent, since it would give a different answer if charCode were truthy and launchIsKeyCode were truthy but not a boolean true.
              >
              > M.
              >
              >
              >
              > ________________________________
              > From: Douglas Crockford <douglas@...>
              > To: jslint_com@yahoogroups.com
              > Sent: Tue, February 2, 2010 5:44:17 AM
              > Subject: Re: [jslint] Why isn't !! OK?
              >
              > --- In jslint_com@yahoogroups.com, "iain_dalton" <iain.dalton@> wrote:
              >
              > > var launchIsKeyCode = this.prefs.getBoolPref(this.kbLaunchNames.userIsKeyCode);
              > > var charCode = event.charCode;
              > > if ((!!charCode) === launchIsKeyCode) {
              > > return;
              > > }
              >
              > I have to confess, this code confuses me.
              > What is it intending to do? Could it be made clearer?
              >
              >
              >
              > ------------------------------------
              >
              > Yahoo! Groups Links
              >
              >
              >
              > [Non-text portions of this message have been removed]
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.