Reputation: 692
What is the use of "?" mark ? can anyone explain what question mark is doing here.
0 ? 1 : 1+w
and also ":" symbol . How to use "?" while writing the codes..
I have seen so many codes where people will use "?" like example above.
can anyone explain what question mark is doing.
Upvotes: 0
Views: 205
Reputation: 56
The expression is a tenerary operator. It works like an 'if' and 'else'. Whatever is before the '?' is evaluated, returning what is after the '?' when the condition is true, and whatever is after the ':' if it is false.
These expressions give the same results:
a = 0 ? 1 : 1+w
if(0){
a = 1;
}else{
a = 1 + w;
}
Upvotes: 0
Reputation: 283
That's called ternary operator, a kind of conditional operator
var fooNotNull = (foo !== null) ? true : false;
if the first condition is 'true', it is saved in the variable 'fooNotNull', else the second value 'false' is stored in 'footNotNull'
Upvotes: 0
Reputation: 10896
It has nothing to do with jquery. it is javascript if else
shorthand method
Check below example
var big;
if (x > 10) {
big = true;
}
else {
big = false;
}
shorthand
var big = (x > 10) ? true : false;
Upvotes: 1
Reputation: 31912
This is nothing to do with jquery, this is standard javascript. It's known as a ternary operator. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
This:
x = a ? b : c
is the equivalent of writing:
if (a) {
x = b
} else {
x = c
}
Upvotes: 3