This question already has answers here:
JavaScript ternary operator example with functions
(6 answers)
What does the question mark and the colon (?: ternary operator) mean in objective-c?
(13 answers)
Closed 8 years ago.
I have two questions:
1. I have a JavaScript function code:
var firstOrNull = function (elems){
return (elems.length > 0 ) ? elems[0] : null;
}
What does ? and : means in this code?
2. What is the meaning of this code:
var stopEvent = function(event){ event.stopPropagation() }
This is Conditional Operator
stopPropagation method of javascript event. It uses to prevent further propagation of the current event.
? and : pair indicate ternary operator in Javascript.
(elems.length > 0 ) ? elems[0] : null; line means if elems length is greater than zero then return elems[0] otherwise return null.
This is called the ternary operators
if(elements.length > 0){
return elems[0];
} else {
return null;
}
is equivalent to :
return (elems.length > 0 ) ? elems[0] : null;
ternary operators
Related
This question already has answers here:
Precedence: Logical or vs. Ternary operator
(2 answers)
Closed 3 years ago.
payload = {type: 3}
const type = payload.type || state.active === "period" ? 1 : 2;
// type returns 1
I'm surprised by the return of type which is 1.. I was expecting it to be 3.. What happened here? What I want to really achieve is if the type index is not available then state.active === "period" ? 1 : 2 will be the basis of the value of type..
How to achieve this in a clean one line?
You need parentheses, because of the operator precedence.
const type = payload.type || (state.active === "period" ? 1 : 2);
This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 6 years ago.
I have seen jQuery code's like this
return 13 == t.keyCode ? (t.preventDefault(), !1) : void 0
What that mean for ? and : ?
Please give me reference, because I still newbie in jQuery. Thank's a lot
Its ternary operator.
Shorthand for writting if/else
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It is a shorthand for if else:
Translation:
if(13 == t.keyCode) { return (t.preventDefault(), !1); } else { return void 0; }
This question already has answers here:
Angularjs if-then-else construction in expression
(5 answers)
Closed 7 years ago.
If I have an angular expression which resolves to a String, can I add a condition to that expression which will append extra text to that String?
I have:
<span>{{groupType}}</span>
But if groupType resolves to car or animal, I want to add an s to the end of the String.
Can I add a condition to the expression to accomplish this?
You can use Ternary operator in Angular expression
<span>{{groupType === 'Car' || groupType === 'Animal' ? groupType + 's' : groupType}}</span>
Yes you can:
<span>{{["Car", "Animal"].indexOf(groupType) >= 0 ? groupType + 's' : groupType}}</span>
I use an array of names with indexOf, because it's a little shorter than "x or y or z"
Ideally, you don't want this logic in your HTML, though. I'd suggest putting it in a function in your controller:
$scope.getGroupType = function(type){
if(["Car", "Anumal"].indexOf(type) >= 0)
return type + 's';
return type;
};
Then, in your HTML:
<span>{{getGroupType(groupType)}}</span>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a Question Mark “?” and Colon “:” Operator Used for?
Question mark in java code
I am writing codes for a RBG to HSV converter.
I have this line:
var d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
i dont really understand what the "?" and the ":" means here.
This is the short way to make a condition :
Condition ? Statment1 : Statement2;
Means
If (Condition) {Statement1} else {Statement2}
Its Ternary Operator:
C = condition? A : B
is equivalent to
if (condition){
C= A;
} else{
C= B;
}
It also support nesting i.e. C = condition1? A : condition2?D:E, which is equivalent to
if (condition1){
C= A;
} else if (condition2){
C= D;
} else{
C= E;
}
This is called ternary operator in java.
Based on java tutorial
Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands.
If first expression results in true, then assign second operand as value, otherwise third operand as value.
It means
if (r==minRGB)
d = g-b
else
if(b==minRGB)
d=r-g
else
d=b-r
In c-based languages, it means:
? :
Its short-hand for an if-else, basically.
it work similar to if than else
if (r==minRGB)
d = g-b;
}else{
if (b==minRGB)
{
d = r-g;
}else{
d = b-r;
}
}
Its called ternary operator (?:): -
System.out.println(condition? value1 : value2);
The above expression is evaluated like: -
if (condition) {
System.out.println(value1);
} else {
System.out.println(value2);
}
This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 8 years ago.
I am working on a project, designed by someone else. I came across the following operation. I have no idea what it is doing. It seems to be returning 1.
Anyone care to elaborate?
Thank you!
( 7 > 8?2:1)
You're looking at the Ternary Operator.
It consists of (condition) ? (expression1) : (expression2). The entire expression will evaluate to (expression1) if (condition) is true, and (expression2) if (condition) is false.
var i = (7 > 8 ? 2 : 1);
translates into
if (7 > 8)
{
i = 2;
}
else
{
i = 1;
}
See: http://en.wikipedia.org/wiki/%3F:
Your example would return 2 if 7 > 8, or 1 otherwise.
? : is a ternary operator. This is equivalent to
var x = 0;
if (7 > 8){
x = 2;
} else {
x = 1;
}
It is a terse way of expressing simple conditional statements. It is a great way to conditionally assign values to a variable without the verbose semantics used above.