I have some logic that switches(with and else/if) true/false with on/off but I would like to make is more condensed and not use a switch statement. Ideally the if/else would be converted into something that is one short line. Thank you!!!
var properties = {}; var IsItMuted = scope.slideshow.isMuted(); if (IsItMuted === true) { properties['Value'] = 'On'; } else { properties['Value'] = 'Off'; }
Writing a one-line if-else statement in JavaScript is possible by using the ternary operator. Running this code prints “Adult” into the console. This code works fine. But you can make it shorter by using the ternary operator.
JavaScript will attempt to run all the statements in order, and if none of them are successful, it will default to the else block. You can have as many else if statements as necessary. In the case of many else if statements, the switch statement might be preferred for readability.
In the logical AND ( && ) operator, if both conditions are true , then the if block will be executed. If one or both of the conditions are false , then the else block will be executed.
Writing a one-line if-else statement in Python is possible by using the ternary operator, also known as the conditional expression. This works just fine. But you can get the job done by writing the if-else statement as a neat one-liner expression.
You want a ternary operator:
properties['Value'] = (IsItMuted === true) ? 'On' : 'Off';
The ? :
is called a ternary operator and acts just like an if
/else
when used in an expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With