Click to See Complete Forum and Search --> : basic javascript question


ukemike
10-10-2003, 07:48 AM
Can someone break down the structure of these types of statements. i dont understand how the logic flows in statements like these:

stdBrowser = (document.getElementById) ? true : false


thanx
mike

requestcode
10-10-2003, 08:42 AM
Maybe this will help:
http://www.javascriptkit.com/javatutors/varshort5.shtml

pyro
10-10-2003, 08:47 AM
In long form, that would be:

if (document.getElementById) {
stdBrowser = true;
}
else {
stdBrowser = false;
}

Basically, it goes like this:

(condition) ? condition true : condition false

Charles
10-10-2003, 12:07 PM
It's called the ternary operator ang it's way useful. Let's say you want to format a number so that a leading zero appears when it is less than 10...

seconds = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() ? new Date().getSeconds();

Or you want to apply a default value...

foo = fooArray[n] : fooArray[n] ? 'default';