I keep seeing this syntax, but I don't know what's it's called or what it does:

Code:

thing == 0 ? "a" : "b" + thing

can someone please explain this?

thanks

11-15-2012, 10:51 PM

Sup3rkirby

That is considered a conditional operator in javascript. Essentially you have 3 partsthing == 0?"a":"b" + thing

The first part is a condition or comparison. If this condition is true then the second part is the value that is returned. If that condition is false then the third and final part is returned instead. So if 'thing' is equal to 0 then that statement would return "a", otherwise it will return "b" + thing.

12-04-2012, 03:58 PM

Jerico

thanks :)

12-05-2012, 04:24 PM

Javaboey

thing == 0 ? "a" : "b" + thing

is shorthand for

if (thing == 0) {
return "a";
} else {
return "b" + thing;
}

12-05-2012, 06:19 PM

ReFreezed

^ Except you don't return from a function when using ?: . A better example might be