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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    if-else shorthand

    I'm looking at some code, and wondering how it works. There are several short hand if-else statements with an extra feature:

    if(condition) return someVariable ? doThis : elseDoThis;

    The code I'm viewing works and processes the entire if-else statement. Can anyone tell me more about the return someVariable portion of this statement? I'd really appreciate it...

    Z.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Actually, return someVariable ? doThis : elseDoThis; is the same as return(someVariable ? doThis : elseDoThis);.

    conditionExpr ? thenExpr : elseExpr is similar to an if..else-statement except all three parts of it are expressions instead of statements.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Examples:-

    var answered = 'yes';
    answered == 'yes' ? alert("You\'ve answered the questions") : alert("You haven\'t answered the questions yet");
    var currentAnswerStatus = (answered == 'yes' ? true : false);

  • #4
    New Coder
    Join Date
    Jun 2005
    Posts
    52
    Thanks
    0
    Thanked 4 Times in 4 Posts
    I think its called a Ternary Condition... if you need something to look up...

  • #5
    New to the CF scene
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the excellent information, but I need a bit more clarification:

    1) in this example: if (return v0) ? return v1 : return v2; -> is v0 ever returned?

    2) what situations would you use such an example?

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by zcountezza View Post
    Thank you for the excellent information, but I need a bit more clarification:

    1) in this example: if (return v0) ? return v1 : return v2; -> is v0 ever returned?
    return is statement level and is not allowed in expression contexts. However, if you remove the return parts, however, then no, v0 is never returned. If v0 evaluates to truthy v1 will be evaluated and returned, otherwise v2 will be evaluated and returned.
    2) what situations would you use such an example?
    In the middle of an expression, where using an if..else-statement would complicate the code. Or just in any place where you don't need statements in your conditionals.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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