Question mark and colon in JavaScript
It is called the Conditional Operator (which is a ternary operator).
It has the form of: condition
? value-if-true
: value-if-false
Think of the ?
as "then" and :
as "else".
Your code is equivalent to
if (max != 0)
hsb.s = 255 * delta / max;
else
hsb.s = 0;
What does the question mark and the colon (?: ternary operator) mean in objective-c?
This is the C ternary operator (Objective-C is a superset of C):
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
is semantically equivalent to
if(inPseudoEditMode) {
label.frame = kLabelIndentedRect;
} else {
label.frame = kLabelRect;
}
The ternary with no first element (e.g. variable ?: anotherVariable
) means the same as (valOrVar != 0) ? valOrVar : anotherValOrVar
What does the question mark mean in js?
It is a ternary operator. It's as if you are doing it like this:
if(change===true){
x.style.background = "red"
}else{
x.style.background = "lime"
}
How multiple question marks ? and colons : in one statement are interpreted in javascript? (Conditional Operators)
Javascript is right-associative, so you 'resolve' the ternaries from right to left.
What is a Question Mark ? and Colon : Operator Used for?
This is the ternary conditional operator, which can be used anywhere, not just the print statement. It's sometimes just called "the ternary operator", but it's not the only ternary operator, just the most common one.
Here's a good example from Wikipedia demonstrating how it works:
A traditional if-else construct in C, Java and JavaScript is written:
if (a > b) {
result = x;
} else {
result = y;
}This can be rewritten as the following statement:
result = a > b ? x : y;
Basically it takes the form:
boolean statement ? true result : false result;
So if the boolean statement is true, you get the first part, and if it's false you get the second one.
Try these if that still doesn't make sense:
System.out.println(true ? "true!" : "false.");
System.out.println(false ? "true!" : "false.");
Javascript colon symbol(:) used in conditon of ternary operator
The first colon is what separates the key from the value in the object you're building (e.g. var o = { foo: "bar" }
).
It may help to rewrite it with parentheses:
var c = {
names: (c.names ? c.names : undefined),
fonts: (c.fonts ? c.fonts : undefined)
};
What does the ? (question mark) mean in javascript?
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;
}
Related Topics
Chrome/Firefox Console.Log Always Appends a Line Saying 'Undefined'
Explain the Encapsulated Anonymous Function Syntax
Getting the Id of the Element That Fired an Event
How to Clone an Array of Objects in JavaScript
What's the Difference Between '$(This)' and 'This'
How to Work Around JavaScript'S Parseint Octal Behavior
Modifying a Copy of a JavaScript Object Is Causing the Original Object to Change
How to Detect Idle Time in JavaScript
Executing ≪Script≫ Elements Inserted With .Innerhtml
How to Get a Subset of a JavaScript Object'S Properties
Addeventlistener Calls the Function Without Me Even Asking It To
Array.Prototype.Fill() With Object Passes Reference and Not New Instance
What Is the !! (Not Not) Operator in JavaScript