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

Splitting a conditional operator (?:) across multiple lines

Expand Messages
  • enaeseth
    I m trying to edit an existing codebase to make it pass JSLint verification so that it can be packed or minified safely. Mostly, my code requires small,
    Message 1 of 4 , Oct 23, 2008
    • 0 Attachment
      I'm trying to edit an existing codebase to make it pass JSLint
      verification so that it can be packed or minified safely. Mostly, my
      code requires small, obvious changes, but JSLint balks at my common
      practice of splitting long statements that use the conditional (ternary)
      operator across three lines, like so:
      this.foo = (typeof(foo) != "undefined") ? foo : true;
      What is a safe and acceptable way to write these statements? Some of
      them are short enough to be written on one line, but most are not.



      [Non-text portions of this message have been removed]
    • enaeseth
      ... Hm, that code snippet didn t post as well as it previewed. Trying again: this.foo = (typeof(foo) != undefined ) ? foo
      Message 2 of 4 , Oct 23, 2008
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "enaeseth" <enaeseth@...> wrote:
        >
        >
        > I'm trying to edit an existing codebase to make it pass JSLint
        > verification so that it can be packed or minified safely. Mostly, my
        > code requires small, obvious changes, but JSLint balks at my common
        > practice of splitting long statements that use the conditional (ternary)
        > operator across three lines, like so:
        > this.foo = (typeof(foo) != "undefined") ? foo : true;
        > What is a safe and acceptable way to write these statements? Some of
        > them are short enough to be written on one line, but most are not.
        >
        >
        >
        > [Non-text portions of this message have been removed]
        >

        Hm, that code snippet didn't post as well as it previewed. Trying again:

        this.foo = (typeof(foo) != "undefined")
        ? foo
        : true;
      • Douglas Crockford
        ... Check the documentation page. JSLint recommends breaking lines after operators to avoid semicolon insertion mishaps. this.foo = typeof(foo) !== undefined
        Message 3 of 4 , Oct 23, 2008
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "enaeseth" <enaeseth@...> wrote:
          >
          >
          > I'm trying to edit an existing codebase to make it pass JSLint
          > verification so that it can be packed or minified safely. Mostly, my
          > code requires small, obvious changes, but JSLint balks at my common
          > practice of splitting long statements that use the conditional (ternary)
          > operator across three lines, like so:
          > this.foo = (typeof(foo) != "undefined") ? foo : true;
          > What is a safe and acceptable way to write these statements? Some of
          > them are short enough to be written on one line, but most are not.

          Check the documentation page. JSLint recommends breaking lines after
          operators to avoid semicolon insertion mishaps.

          this.foo = typeof(foo) !== "undefined" ?
          foo :
          true;
        • Daniel Cassidy
          ... this.foo = (typeof(foo) !== undefined ) ? foo : true; See Line Breaking on the page http://www.jslint.com/lint.html Dan.
          Message 4 of 4 , Oct 23, 2008
          • 0 Attachment
            On Thu, Oct 23, 2008 at 10:40 PM, enaeseth <enaeseth@...> wrote:
            > this.foo = (typeof(foo) != "undefined")
            > ? foo
            > : true;

            this.foo = (typeof(foo) !== 'undefined') ?
            foo :
            true;

            See "Line Breaking" on the page http://www.jslint.com/lint.html

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