Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  36] [ 3]  / answers: 1 / hits: 119113  / 9 Years ago, tue, may 12, 2015, 12:00:00

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';
}

More From » javascript

 Answers
46

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.


[#66641] Friday, May 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nestorjarettg

Total Points: 451
Total Questions: 108
Total Answers: 108

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;