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; }
Related
This question already has answers here:
Can I use chained comparison operator syntax? [duplicate]
(6 answers)
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
I have a question about how chain equality works in JavaScript.
For example in python if you have:
a, b = 1, 2
a == b == False //False
Because it converts to:
(a == b) and (b == False)
So, finally it is False.
But when I try this in js:
console.log(1==2==false) // true
I got "true". I don't know why and how it is worked in js.
could you please help me out?
Reading left to right:
1==2 is false
false==false is true
in the code 1 == 2 == false
we read it as 1 == 2 == false
so basically 1 == 2 is false
and fasle == false is true
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:
Question mark and colon in JavaScript
(8 answers)
Closed 8 years ago.
I'm trying to comment out code that I used from a tutorial but haven't actually seen a ?-mark used in JavaScript...
This is a small part of the code below:
this.year = (isNaN(year) || year == null) ? calCurrent.getFullYear() : year;
What you are referring to is the ternary operator which is an inline conditional statement. To illustrate:
this.year = (isNaN(year) || year == null) ? calCurrent.getFullYear() : year;
is equivalent to
if(isNaN(year) || year == null){
this.year=calCurrent.getFullYear()
}
else{
this.year=year;
}
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
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.