Question : question mark in expression

Good morning expert,

In the script below, in the expression
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
I understand the code until I get the question mark.
It says set a variable called 'date' and make the variable 'date'
equal to getDate, (where getDate returns the day of the month
[for instance today it would return 28]) is less than zero. Thats
where I get lost.

What is the function of the question mark?

Thanks

Allen in Dallas


<SCRIPT LANGUAGE="JavaScript">
// Get today's current date.
var now = new Date();

// Array list of days.
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

// Array list of months.
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');

// Calculate the number of the current day in the week.
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

// Calculate four digit year.
function fourdigits(number)      {
      return (number < 1000) ? number + 1900 : number;
                                                }

// Join it all together
today =  days[now.getDay()] + ", " +
         months[now.getMonth()] + " " +
         date + ", " +
         (fourdigits(now.getYear())) ;

// Print out the data.
document.write(today);

//  End -->
</SCRIPT>

Answer : question mark in expression

For simplicity of understanding the code

var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();

can be broken down to the following lines :

var date = "" ;
if(now.getDate()<10) {
date = "0";
}
date = date + now.getDate();

As we can observe we can reduce the amount of code to be written using the ? operator.
Random Solutions  
 
programming4us programming4us