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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

What does ? mean in Javascript.

  • 06-03-2014 9: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: 195 ✭✭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