xxxxxxxxxx
const num1 = 3;
const num2 = 3;
num1 == num2 ? console.log('true') : console.log('flase');
//Expected output: true
xxxxxxxxxx
// (condition) ? (if true, do this) : (otherwise, do this)
const hasAccount = true
// let userDisplayMessage
// if (hasAccount) {
// userDisplayMessage = 'Welcome Back'
// } else {
// userDisplayMessage = 'Please Sign Up'
// }
const userDisplayMessage = hasAccount ? 'Welcome Back' : 'Please Sign Up'
console.log(userDisplayMessage)
xxxxxxxxxx
// syntax:
condition ? console.log("true") : console.log("false");
// e.g:
let n = 15;
n % 2 === 0 ? console.log("even number") : console.log("odd number");
xxxxxxxxxx
let moneyAmount = 80;
let creditCardAmont = 70;
let drink = (moneyAmount > 60 && creditCardAmont > 50) ? 'buy coke' : 'filter water';
console.log(drink)
xxxxxxxxxx
condition ? expression-if-true : expression-if-false;
function findGreater(a, b) {
return a > b ? "a is greater" : "b is greater";
}
xxxxxxxxxx
const number = 25;
const isEven = (number % 2 === 0) ? "Yes" : "No";
console.log("Is " + number + " an even number? " + isEven);
xxxxxxxxxx
variable = Condition? Expression1 : Expression2
if expression is stated by condition is true = Expression1
if expression is stated by condition is false = Exptession2
public static void Main(string[] args)
{
int number = 11;
String result;
result = (number % 2 == 0) ? "Even number" : "Odd number";
Console.WriteLine(result);
}
xxxxxxxxxx
if ($scope.SearchSalesOrderAndCompanyName !== undefined && $scope.SearchSalesOrderAndCompanyName != null ) {
SearchCriteria += " AND [SalesOrderNo] LIKE '%" + $scope.SearchSalesOrderAndCompanyName + "%' OR ([SO].[CompanyNameOnBill] LIKE '%" + $scope.SearchSalesOrderAndCompanyName + "%')";
}
if ($scope.FromDate !== undefined && $scope.FromDate !=null) {
SearchCriteria += " AND [SO].[SalesOrderDate] LIKE '%" + $scope.FromDate + "%'";
}
xxxxxxxxxx
condition ? exprIfTrue : exprIfFalse
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy