Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

308
Views
One line if/else in JavaScript

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';
}       
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

You can likely replace your if/else logic with the following to give you a "one-liner"

properties['Value'] = scope.slideshow.isMuted() ? 'On' : 'Off';

see Conditional (ternary) Operator for more info

over 4 years ago · Santiago Trujillo Report

0

var properties = {"Value":scope.slideshow.isMuted() && "on" || "off"}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!