
$message = 'Hello '.($user->isLoggedIn() ? $user->name : 'Guest') Įcho 'You have ',($score > 10 ? 'passed' : 'failed') Įcho 'You have ',($age > 10 ? ($score < 80 ? 'failed' : 'Passed') : ($score < 50 ? 'failed' : 'passed')) // returns 'You are passed'
PHP IF ELSE STATEMENT SHORTHAND CODE


Output: Fast Code language: JavaScript ( javascript ) Let message = speed >= 120 ? 'Too Fast' : speed >= 80 ? 'Fast' : 'OK'
PHP IF ELSE STATEMENT SHORTHAND HOW TO
The following example shows how to use two ternary operators in the same expression: let speed = 90 Let canChange = locked != 1 Code language: JavaScript ( javascript ) 3) Using multiple JavaScript ternary operators example In this case, you can simplify it by using a Boolean expression as follows: let locked = 1 If the locked is 1, then the canChange variable is set to false, otherwise, it is set to true. Let canChange = locked != 1 ? true : false Code language: JavaScript ( javascript ) See the following example: let locked = 1 In this example, the returned value of the ternary operator is the last value in the comma-separated list. redirect to nextURL here console.log(nextURL) // '/admin' Code language: JavaScript ( javascript ) ? (alert( 'You will redirect to admin area'), '/admin') The following example uses the ternary operator to perform multiple operations, where each operation is separated by a comma. 1) Using the JavaScript ternary operator to perform multiple statements

Let’s take some examples of using the ternary operator. In this syntax, if the condition is true, the variableName will take the result of the first expression ( expressionIfTrue) or expressionIfFalse otherwise. The following shows the syntax of the ternary operator used in an expression: let variableName = condition ? expressionIfTrue : expressionIfFalse Code language: JavaScript ( javascript ) If it is false, the second expression ( expressionIfFalse) executes. If the condition is true, the first expression ( expresionIfTrue) executes. In this syntax, the condition is an expression that evaluates to a Boolean value, either true or false. Here’s the syntax of the ternary operator: condition ? expressionIfTrue : expressionIfFalse Code language: JavaScript ( javascript ) Message = age >= 16 ? 'You can drive.' : 'You cannot drive.' Or you can use the ternary operator in an expression as follows: let age = 18 Alternatively, you can use a ternary operator instead of the if-else statement like this: let age = 18 Īge >= 16 ? (message = 'You can drive.') : (message = 'You cannot drive.') In this example, we show a message that a person can drive if the age is greater than or equal to 16. For example: let age = 18 Ĭonsole.log(message) Code language: JavaScript ( javascript ) When you want to execute a block if a condition evaluates to true, you often use an if…else statement. Introduction to JavaScript ternary operator Summary : in this tutorial, you will learn how to use the JavaScript ternary operator to make your code more concise.
