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: This … Read more

What is the Java ?: operator called and what does it do?

Yes, it is a shorthand form of It’s called the conditional operator. Many people (erroneously) call it the ternary operator, because it’s the only ternary (three-argument) operator in Java, C, C++, and probably many other languages. But theoretically there could be another ternary operator, whereas there can only be one conditional operator. The official name is given in the Java Language … Read more

What does the question mark character (‘?’) mean in C++?

This is commonly referred to as the conditional operator, and when used like this: … if the condition evaluates to true, the expression evaluates to result_if_true, otherwise it evaluates to result_if_false. It is syntactic sugar, and in this case, it can be replaced with Note: Some people refer to ?: it as “the ternary operator“, because it is the only ternary operator (i.e. operator that … Read more

How do you use the ? : (conditional) operator in JavaScript?

This is a one-line shorthand for an if-else statement. It’s called the conditional operator.1 Here is an example of code that could be shortened with the conditional operator: This can be shortened with the ?: like so: Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of … Read more

How do you use the ? : (conditional) operator in JavaScript?

This is a one-line shorthand for an if-else statement. It’s called the conditional operator.1 Here is an example of code that could be shortened with the conditional operator: This can be shortened with the ?: like so: Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of … Read more