SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: ? Operator

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ? Operator

    Could someone explain to me what this line of code means:

    function1(num) ? 13 : 12;

    I think it means

    if function1(num) = 12 then function1(num) = 12

    What is your input?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    That code is incomplete because it isn't doing anything with the value so with just that it would be equivalent to

    Code:
    if (function1(num)) 13;
    else 12;
    In order to actually do something with the numbers you'd need to assign the whole thing to something for example:

    Code:
    x = function1(num) ? 13 : 12;
    which is the same as

    Code:
    if (function1(num)) x = 13;
    else x = 12;

    You could easiy test it for yourself by picking two different values for num where one causes function1() to return true and the other has it return false.
    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="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    x = function1(num) ? 13 : 12;
    So some values:
    if function1(num) =13
    x=12

    if function1(num) = 12
    x = 12

    if function1(num) = 5
    x = 5

    if function1(num) = 15
    x = 15

    if function1(num) = n
    x = n unless n=13 then x=12

  4. #4
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This is just ternary operator.

    If num is TRUE or 1 assign 13 else assign 12
    It is the simplified version of if and else.

    Just the basic.
    Quality codes are optimized and tested...
    Click here for inspiration..

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It all depends on what is in function1.

    If function 1 is defined as:

    Code:
    function1 = function(num) {return isFinite(num);};
    then

    Code:
    x = function1(num) ? 13 : 12;
    would set x to 13 if num contains a finite number and to 12 if it contains infinity or something other than a number.

    If function 1 is defined as:

    Code:
    function1 = function(num) {return false;};
    then it would set x to 12 regardless of what is in num.


    You haven't showed us your function1() so what num needs to be to return true (and so set a value of 13) and what it needs to be to return false (and set a value of 12) are completely unknown to us.

    If function 1 is defined as:

    Code:
    function1 = function(num) {return 12===num ? true : false;};
    then

    Code:
    x = function1(num) ? 13 : 12;
    would set x to 13 if num is 12 and set x to 12 if num is anything at all except 12.
    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="^$">

  6. #6
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Philosophaie View Post
    So some values:
    if function1(num) =13
    x=12

    if function1(num) = 12
    x = 12

    if function1(num) = 5
    x = 5

    if function1(num) = 15
    x = 15

    if function1(num) = n
    x = n unless n=13 then x=12
    First understand that the comparison operator is == not =, otherwise you'll soon be back asking another question.

    I still don't think you understand:

    The statement
    Code:
    function1(num) ? 13 : 12;
    evaluates to 13 when function1(num) returns a non-zero value, otherwise 12.
    Tab-indentation is a crime against humanity.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    The statement
    Code:
    function1(num) ? 13 : 12;
    evaluates to 13 when function1(num) returns a non-zero value, otherwise 12.
    Or more accurately, when function1(num) returns a non-falsy value, then the above code evaluates to 13.
    If function1(num) is a falsy value (false, undefined, null, NaN, 0, ""), then the above code evaluates to 12.

    The question mark is a conditional operator.
    condition ? expr1 : expr2

    You can see that above link for further details.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •