Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post

    return with conditional operator

    I've been simplifying my scripts with conditionals and cutting down on if and else. I know you can use the conditional operator as follows when declaring a value of a variable:

    Instead of:
    Code:
    if(true)
    	{
    	X="Yes"
    	}
    else
    	{
    	X="No"
    	}
    Do this:

    Code:
    X=(true)?"Yes":"No"
    Can it also be used as a sort of inline function in the middle of a statement? Such as a return?

    Instead of:

    Code:
    if(true)
    	{
    	return 1
    	}
    else
    	{
    	return 0
    	}
    Can this be done?

    Code:
    return (true)?1:0
    It worked in FireFox and IE, and I was told it was valid in C++ too. I just want to be sure that it isn't a bad coding practice that won't break across browsers. Is this a good idea or should it be avoided?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Well a functions default is return true so you would really only need to specify false.
    Code:
    if(!true)
    {
    return false;
    }
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    If you need to return strictly the numbers 0 or 1, you may use the Number() method

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script type="text/javascript">
    function 
    returnNumber(Bool){
    return 
    Number(Bool)
    }
    </script>
    </head>
    <body>
    <input type="button" value="TRUE" onclick="alert(returnNumber(true))">
    <br>
    <input type="button" value="FALSE" onclick="alert(returnNumber(false))">
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    actually if you try this in your error console in firefox 1.5:

    Code:
    function x() {var q = 5;} alert(x());
    The result is you alert undefined, not true as aero stated.

    @Aero: where is what you said true? Is it perhaps not defined in the standard and therefore each browser can implement it differently. If so, it's completely unreliable for anything.

    To answer the OP's question:

    Ternary or inline conditions, in JavaScript, are evaluated like values, not like functions. It almost behaves like a function, but not quite. For example:

    5

    The statement above is a valid JavaScript token and it evaluates to the integer value 5.

    q = 5;
    q;

    The second statement above is a valid javascript token and it evaluates to the integer value of 5 as well.

    (true ? 5 : 6)

    This is a composite token, (it contains several tokens), and it also evaluates to the integer value of 5.

    So you can use them as the right-hand side of statements like assignments, conditionals, and returns.

    Code:
    if (q == (something ? 5 : 6))
    {
      newValue = (color ? 'blue' : (shape ? 'square' : null));
    }
    
    return (newValue == null ? false : newValue);


  •  

    Posting Permissions

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