Possible Duplicate:
javascript ? : notation
what does the "?" operator mean?
Possible Duplicate:
javascript ? : notation
what does the "?" operator mean?
it means an inline if
condition ? true_statement : false_statement
e.g
if(condition){
alert("true");
}else{
alert("false");
}
is the same as condition ? alert("true"): alert("false");
It, along with :
comprises the ternary operator
and is a shortcut for returning one of two values (the second and third sub-expressions), based on the result of the condition (the first sub-expression).
Wikipedia gives a good description: http://en.wikipedia.org/wiki/%3F:#Javascript
It's used like this:
var result = (condition ? value_for_true : value_for_false);
Example:
var result = (1 > 0 ? "It is greater" : "It is less");
The above example stores "It is greater"
in the variable result
.
On its own, ?
does nothing except cause a syntax error when used without :
.
It's part of the ternary operator.
// This simple if
if (25 > 23) {
alert("yes");
} else {
alert("no");
}
// Is the same as
alert(25 > 23 ? "yes" : "no");
You probably mean the ?:
, or ternary, operator. Since this has been covered multiple times before, I'll refer you to this thread for a full explanation.