Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

What does ? mean in Javascript.

  • 06-03-2014 09:31PM
    #1
    Registered Users, Registered Users 2 Posts: 266 ✭✭


    Can anyone tell me what the question mark indicates in the Javascript code below?
          for(var i = 0; i < 10; i++){
    	
            var s = parseInt(scores[i]);
    	var val = (!isNaN(s) ? s : 0 );
    
          }
    

    I have search everywhere but can not find any reference to ? in Javascript.
    It is from a HTML5 game which can be sourced here:
    http://www.developerdrive.com/2013/09/html5-javascript-gem-game-with-saved-scoreboard/

    Thanx for any help.


Comments

  • Registered Users, Registered Users 2 Posts: 1,144 ✭✭✭Ballyv24


    In Java the following is the case (so presume JavaScript is the same)

    if (a > b) {
    max = a;
    }
    else {
    max = b;
    }

    is the same logic as:

    max = (a > b) ? a : b;

    so in English.. if the statement in brackets is true.. then max is given the value of the a variable , otherwise max is given the value of the b variable

    Source: http://www.cafeaulait.org/course/week2/43.html


  • Registered Users, Registered Users 2 Posts: 197 ✭✭dumb_parade


    It's similar to if/else
    Syntax is (test some Boolean operation ? Return this value if true : return this value if false)
    In this case it checks if s is a number(isNaN(s)) or not. If it's not it sets val to 0.


  • Registered Users, Registered Users 2 Posts: 262 ✭✭Banta


    Answered above as I was posting... in more detail I might add.


  • Registered Users, Registered Users 2 Posts: 266 ✭✭Gerb68


    Perfect. Thanx a lot guys. Appreciate it.


  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭smcelhinney


    Its called the ternary operator.

    http://en.wikipedia.org/wiki/Ternary_operation


  • Advertisement
Advertisement