Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

What does ? mean in Javascript.

Options
  • 06-03-2014 10:31pm
    #1
    Registered Users 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 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 Posts: 194 ✭✭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 Posts: 262 ✭✭Banta


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


  • Registered Users Posts: 266 ✭✭Gerb68


    Perfect. Thanx a lot guys. Appreciate it.


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


    Its called the ternary operator.

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


  • Advertisement
Advertisement