| code Python module
| COM PHP module
| Ev PHP module
| FFI PHP module
| imp Python module
| io Python module
| Lua PHP module
| operator Python module
| os Python module
| Python functions
| re Python module
| resource Python module
| site Python module
| stat Python module
| struct Python module
| test Python module
| time Python module
| UI PHP module
👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
There may be times when you want certain code to run only when certain conditions are met.
For example, let’s say you have a date of birth field on your e-commerce site. Also suppose you only allow customers over 18 to buy products from your website. We recommend that you verify that a user is the correct age before using your site.
The potential result is true or false, so we can use a ternary operator to check if a user is the correct age.
In this tutorial, we will see how to use ternary operators. We’ll refer to an example to get you started.
The ternary expression has the following syntax:
A condition comes first, then a question mark. The first expression will be executed if the condition is met. The second condition will be executed if the condition is not met.
Back to basics: if statements
Here is an example of an if statement. Let’s use the scenario of an ecommerce site checking the age of its users that we talked about earlier.
Our if statement will be executed and user_is_right_age will be set to false. The program first checks if the client’s age is 18. Since the user is no older than 18, the else clause is executed and sets user_is_right_age to false.
Even though this code works, it’s a lot of code ! It takes five lines to verify the age of our client.
What if t I tell you that there is a way to perform the same function in one line of code ? This is where the ternary operator comes in.
We can simplify our previous if statement by using a ternary operator:
This code, when executed, gives the same result as the previous code (false), but in fewer lines. If the conditional was true, if the user’s age was 18 or older, our program would return true.
In the example we used above, our program checks if the user is the correct age. Our code returns false because the user is under 18 and our condition has not been met.
Our ternary operator will check if the user is an express customer and then determine the shipping time for the user. As seen above, our user is an express customer, so the condition is rated as true, they are delivered in 48 hours. If our customer is not an express customer, our code will be wrong. Therefore, the customer will be delivered within 72 hours.
Let’s say we are an e-commerce company that offers three delivery times: standard (72), express (48 hours) and one day (24 hours). How to use a ternary operator in this case, since there are three options ? Should we use an if‚Ä¶ else statement instead ?
No don’t worry, you can also use a JS ternary operator with three expressions. Here is an example of the previous example in action:
In this case, the delivery time for our client would be 24 hours. This program first checks whether the user is an express customer, which he is not. So, check if these are 24 hour customers, which is true. Therefore, the shippingTimeForCustomer variable is assigned the value 24.
If you were not an express or 24 hour customer, the delivery time would be 72 hours.