How to Replace "If" Statement With a Ternary Operator ( : )

How to replace if statement with a ternary operator ( ? : )?

The

(condition) ? /* value to return if condition is true */ 
: /* value to return if condition is false */ ;

syntax is not a "shorthand if" operator (the ? is called the conditional operator) because you cannot execute code in the same manner as if you did:

if (condition) {
/* condition is true, do something like echo */
}
else {
/* condition is false, do something else */
}

In your example, you are executing the echo statement when the $address is not empty. You can't do this the same way with the conditional operator. What you can do however, is echo the result of the conditional operator:

echo empty($address['street2']) ? "Street2 is empty!" : $address['street2'];

and this will display "Street is empty!" if it is empty, otherwise it will display the street2 address.

How can I change the if else statement in the function with Ternary operator in JavaScript?

The code is very inefficient because you find the thing, and then you turn around and find the thing again. So you end up looping multiple times.

To make it more readable, I broke it up into parts. It also loops one time over the object to locate the items with children and the items without children. There is a ternary operator there to handle with and without.

The code then determines if it is the child or not and grabs the object.

// Grab the package
var selectedPackageProducts = this.deal.packages.find(p => p.isSelected).dealProducts;

// check to see if the children has the product type or if the parent does (if no children)
const selectedProduct = selectedPackageProducts.find(dp =>
dp.children.length > 0 ?
dp.children[0].product.productTypeCode === productType :
dp.product.productTypeCode === productType)

// If we have children use it, else reference the parent
const productObj = selectedProduct && selectedProduct.children.length ?
selectedProduct.children[0] :
selectedProduct;

// get the product name
const productName = productObj && productObj.product.name

replace '.' with '' in ternary operator

var arr = '.net'
arr = arr.startsWith('.')
? arr.slice(1)
: arr

You probably don't want to replace the . as there could be a . elsewhere in the string. .slice will just remove the first character and return the rest.

Why can't I replace if statement with conditional operator (?:)?

The syntax is:

condition ? value1 : value2;

not

condition ? statement1 : statement2;

The conditional operator is an expression, not a statement. It doesn't execute statements like an if statement does: it returns a value.

What you mean is:

preferredClass = (preferredClass == PlaneClass.FIRST_CLASS ? 
PlaneClass.ECONOMY_CLASS : PlaneClass.FIRST_CLASS);

Change from if else to ternary operator

It's not generally a good idea to use nested conditional operators, but it can be done:

printf("%s", a==b ? "equal" : (a > b ? "bigger" : "smaller"));


Related Topics



Leave a reply



Submit