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

Yes, it is a shorthand form of int count; if (isHere) count = getHereCount(index); else count = getAwayCount(index); 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, … 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: var userType; if (userIsYoungerThan18) { userType = “Minor”; } else { userType = “Adult”; } if (userIsYoungerThan21) { serveDrink(“Grape Juice”); } else { serveDrink(“Wine”); } This can … Read more