SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ternary operator

    It's well know that the ternary operator sintax is something like so:
    Code:
    (test) ? true doThis : false doThat
    Suppose that in case condition is false we want do nothing. How to code "do nothing"? May I let it blank or there are an appropriate sintax?
    Mauricio Samy Silva
    http://www.maujor.com/

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes

    Code:
    (test) ? true doThis : ""

  3. #3
    SitePoint Member
    Join Date
    Sep 2005
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    test && true doThis

  4. #4
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need a ternary operator in this situation. Use an if() statement instead.

  5. #5
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks to all for the reply
    derfleurer & joebert
    I need a ternary operator syntax.
    RNEL
    This works, but is this a syntactically correct solution under the JavaScript language view point? or is there a more elegant solution?
    Mauricio Samy Silva
    http://www.maujor.com/

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    A ternary operator doesn't have a do nothing option for either the true or false path since the entire ternary command returns a value and so both paths must set a value.

    The "do nothing" value will depend on what value you want the ternary operator to return to indicate "do nothing".
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Maujor View Post
    Many thanks to all for the reply
    derfleurer & joebert
    I need a ternary operator syntax.
    RNEL
    This works, but is this a syntactically correct solution under the JavaScript language view point? or is there a more elegant solution?
    How about this?

    Code javascript:
    if (test) {doThat}

    The semicolon is optional. If you're doing a one-liner with one statement, it can look cleaner without the semicolon.

    You more commonly see it expanded out, such as:

    Code javascript:
    if (test) {
        doThat;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you could stick NULL in there, but it's like putting pepper on a sugar cookie.

    Code:
    function f(a){alert(a);}
    var y = true;
    y ? f('y') : null;
    !y ? null : f('!y');
    If you actually do need a ternary, you're leaving out something important from the information you're giving us. Based on what you've told us, you do not need a ternary operator.

  9. #9
    SitePoint Evangelist Maujor's Avatar
    Join Date
    Mar 2005
    Location
    Rio de Janeiro - Brazil - South America
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joebert View Post
    ...If you actually do need a ternary, you're leaving out something important from the information you're giving us...
    Sorry, my fault. The question is a theoretical one regarding the JavaScript syntax for ternary operator.
    Now, in conclusion I think that the following syntaxes works but isn't a good solution:
    Code:
    (test) ? doThis : "";
    (test) ? doThis : null;
    In this case the ternary operator isn't the best solution and the syntax should be:
    Code:
    if (test) {
         doThis
    }
    Or am I missing something? Any others thoughts?
    Mauricio Samy Silva
    http://www.maujor.com/

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Maujor View Post
    Or am I missing something? Any others thoughts?
    There is difference of semantics in regards to ternary operators. It's better when they are used to just to assign values.

    If you want to run a different set of statements or functions, staying with the if/else structure is a better choice.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Maujor View Post
    Or am I missing something?
    Yes, you are missing the first part of a statement containing a ternary operator.

    The ternary operator works in a statement like this:

    Code:
    someVariable = (test) ? doThis : doThat;
    and is the equivalent of:

    Code:
    if (test)
       someVariable = doThis;
    else
       someVariable = doThat;
    It is to simplify the assignment of a value to someVariable that you use a ternary operator and so there is no "do nothing" case since the purpose of a ternary operator is to select which of two values to assign. If you only have one value to assign then it simplifies to

    Code:
    someVariable = doThis;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

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