How to write ternary operator logic for multiple conditions and for optional param?

ternary operator multiple conditions javascript
conditional (ternary) operator
nested ternary operator javascript
ternary operator c#
ternary operator typescript
ternary operator python
ternary operator php
ternary operator c++

trying to write logic for my validator functions that has objects we use as errorMap for the input validation , For below logic args.drugName is optional field if user provide text we just want to make sure its greater than 3 letters or in case of empty successCondition should be valid. So for the optional param in typescript how do we fix this issue ?


                errorKey: ValidationErrorEnum.InvalidDrugName,
                successCondition: (args: DrugPriceParam) => {
                    let isValid: boolean = false;
                    isValid = args.drugName.length >= 3 ? true : _.isEmpty(args.drugName) ? true : false;

                    // if (args.drugName && args.drugName.length >= 3) {
                    //         isValid = true;
                    // } else if (_.isEmpty(args.drugName)) {
                    //     isValid = true;
                    // }
                    return isValid;


error TS2532: Object is possibly 'undefined'.

You could simplify the check to

return !args.drugName || args.drugName.length > 2;
//     if empty
//                       if longer then 2 characters

Conditional (ternary) operator, Syntax. condition ? exprIfTrue : exprIfFalse. Parameters. condition: An expression whose value is� I had the same question at my study. Thanks for the info about if and else.Would be my choice too except the assignment is asking us specificly to use the conditional operators. so basically they're asking us to write it in an unreadable way.

Its same as you write if condition

 (args.drugName && args.drugName.length >= 3) ? true : _.isEmpty(args.drugName) ? true : false;

There is new feature coming in js - optional chaining. you can use this currently using webpack plugin.

Using this we can simply write

args?.drugName?.length >= 3 ? true : _.isEmpty(args.drugName) ? true : false;

Conditional branching: if, '?', The if statement may contain an optional “else” block. It executes when the condition is false. For example: let year =� 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. This operator is frequently used as a shortcut for the if

Because you're simply returning booleans, you don't actually need to use the ternary operator. You should be able to just pass the result of the comparison operations:

// if (args.drugName && args.drugName.length >= 3) {
//     isValid = true;
// } else if (_.isEmpty(args.drugName)) {
//     isValid = true;
// }

// Becomes:

isValid = (args.drugName && args.drugName.length >= 3) || _.isEmpty(args.drugName);

Don't forget about short-circuiting, either: If the first half of the || is found to be true, then _.isEmpty won't even be run (and similarly, if the first half of the && is found to be false, then ...length >= 3 won't even be run).

Make Your Code Cleaner with JavaScript Ternary Operator, This tutorial shows you how to use the JavaScript ternary operator as the The condition is an expression that evaluates to a Boolean value, either true or false . the ternary operator in ES5 is to set default parameters of a function, for example: If the logic contains many ifelse statements, you shouldn't use the ternary� Then, the ternary operator is used to check if number is even or not. Since, 2 is even, the expression (number % 2 == 0) returns true. We can also use ternary operator to return numbers, strings and characters. Instead of storing the return value in variable isEven, we can directly print the value returned by ternary operator as,

C# Ternary Operator (?:) with Examples, In c# ternary operator is a substitute for if else statement and return a values the above example, we simplified if…else condition by replacing multiple lines of � But notice the “else” conditions return null. This is becasue a ternary expects an else condition. For simple if conditions, we could use something a little more fitting: the logical AND operator. Here’s the same code written using a logical AND.

The Java ternary operator examples, One use of the Java ternary operator is to assign the minimum (or Note that the parentheses in this example are optional, so you can write that same statement like this: be used to avoid replicating a call to a function with a lot of parameters : front page alvin on twitter search privacy terms & conditions. Ternary operator can be considered as the replacement of if-else statements in C++. The other name for the ternary operator is conditional operator, that is perhaps more meaningful, as it evaluates the conditions just like if. Moreover, the ternary operator makes the code more concise without compromising readability.

What is the Best Style for Functions with Multiple Returns and If/Else , I prefer this one: return (condition) ? value : other_value;. but it's just a matter of personal preference. On any team I've worked I was happy to comply with� If you are going to nest, save yourself some grief and wrap the nested ternary in parenthesis. To return a nested ternary, The whole thing can be wrapped in parenthesis. Normally, it’s not a good practice to wrap return values, but in this case, it’s the safest way to ensure correct returns.