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

178
Views
alternatives for multiple if else conditions to make it in single line - javascript

In one function, i am checking couple of condition with multiple if else condition. I want to do with using ternary operator to make it single line. I am not able to configure it how to do that

if (!this.$route.params.prdKey) {
            this.prodData.prdKey = '';
        } else {
            this.prodData.prdKey = this.$route.params.prdKey;
        }
        if (!this.$route.params.version) {
            this.prodData.version = 1;
        } else {
            this.prodData.version = this.$route.params.version;
        }
        this.pageMode = this.$route.params.pageMode;
        this.getProductDetailsList();
        if (!this.isPrdAvailable) {
            this.getproductList();
        } else {
            this.loadMoreproducts();
        }

Any other approach is also ok for me. There are in other part where i am using this kind of if-else condition check multiple times. So, i can remove those as well.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

|| can be used to alternate between two possibilities - if the left-hand side is truthy, it'll evaluate to it, otherwise it'll evaluate to the right-hand side.

this.prodData.prdKey = this.$route.params.prdKey || '';
this.prodData.version = this.$route.params.version || 1;
about 4 years ago · Juan Pablo Isaza Report

0

You could use two ternary expressions here:

this.prodData.prdKey = this.$route.params.prdKey ? this.$route.params.prdKey : '';
this.prodData.version = this.$route.params.version ? this.$route.params.version : 1;
about 4 years ago · Juan Pablo Isaza 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!