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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Location
    Brussels Belgium
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Who has ever seen a condition written this way?

    The "?" seems to be called the conditional operator.

    Code:
    var points = prompt('How many points do you want on ten?');
    var result = (points >= 5) ? "succeeded!" : "didn't succeed ;-)";
    window.alert("You " + result);
    Last edited by goalsurfer; 01-19-2012 at 02:05 PM. Reason: replaced script tags by[CODE][/CODE]

  • #2
    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
    This is called a ternary operator. As the name indicates ternary operators take three operands. The syntax is condition ? result1 : result2;
    It is often used as a shortcut for an if..else statement.

    For more info Google, e.g.

    http://www.hscripts.com/tutorials/ja...pt/ternary.php

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Nov 2009
    Location
    Brussels Belgium
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    All right, that'll do it.


  •  

    Posting Permissions

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