
If the boolean_expression evaluates to NULL, the expression result is NULL. The data type of expression1 or expression2. Is any valid expression that evaluates to TRUE, FALSE, or NULL. Syntaxīoolean_expression?expression1:expression2

If the Boolean expression evaluates to FALSE then the second expression is evaluated and its result is the expression result. If the Boolean expression evaluates to TRUE, then the first expression is evaluated and the result is the expression result. Returns one of two expressions based on the evaluation of a Boolean expression. If you enjoy this tutorial, you may want check out my website at for more JavaScript goodies.Īlso, I have a free weekly newsletter about web development tutorials (mostly JavaScript related).SSIS Integration Runtime in Azure Data Factory I hope it has helped you to understand how the ternary operator works. It's best to stick with either if/else or switch statements for such cases. However, it's not recommended to replace multiple if/else statements with multiple ternary operators because it makes the code harder to read in the future. Here's one way to write the program: let score = 85 Ĭonsole.log(`Your exam grade is $`) Multiple ternary operators in action



The syntax for the conditional operator looks like this: conditional ? expression_when_true : expression_when_false conditional operator basic syntaxįirst, you need to write a conditional expression that evaluates into either true or false. The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. This tutorial will help you learn how to replace an if/else statement with a more concise shorthand syntax called the ternary operator.
