0

Instead of if...else statement how to do using ternary operation or any alternative way to simplify code in javascript

if(req.query.pr=="trans"){
    util.transUrl(req.originalUrl).then(param => {
      res.redirect(param);
    })
  }
  else if(req.query.pr=="inst"){
    util.instaUrl(req.originalUrl).then(param => {
      res.redirect(param);
    })
  }
  else{
    res.status(400).send("Contact the editor of the originating page.")
  }
4
  • 6
    If there are multiple lines of code to be executed, if else if loop is better readable than ternary operators Commented Apr 26, 2019 at 5:50
  • Try to read more about ternary operators. Here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… and codeburst.io/… Then decide whether to use it or not. Commented Apr 26, 2019 at 5:53
  • 1
    The conditional operator is not very appropriate here. I suppose the first if and else if could be condensed, but I prefer the current version, it's more readable. To make things a bit more DRY you could define a function param => res.redirect(param) Commented Apr 26, 2019 at 5:53
  • Ternary operators are better to be used for simplest (one liners) if...else conditions. if you want to use it just for reducing number of lines then you can have a look at en.wikipedia.org/wiki/Minification_(programming) Commented Apr 26, 2019 at 6:08

1 Answer 1

2

There are multiple ways to write a conditional statement in JS. However, if there are multiple statements I would say you should stick to if else if else. But if you want to see other approaches, here they are:

Using Ternary operator ? :

const {pr} = req.query
pr === 'trans'
? util.transUrl(req.originalUrl).then(param => 
      res.redirect(param)
  )
: pr === 'inst'
? util.instaUrl(req.originalUrl).then(param => 
      res.redirect(param)
  )
: res.status(400).send('Contact the editor of the originating page.')

Using Gate logic && ||

const {pr} = req.query
(pr === 'trans' && 
    util.transUrl(req.originalUrl).then(param => 
        res.redirect(param))
) ||
(pr=== 'inst' && 
    util.instaUrl(req.originalUrl).then(param => 
        res.redirect(param))
) || 
res.status(400).send('Contact the editor of the originating page.')

Now, Looking at your code, here if and else if statements are similar. So you can avoid else if using ternary operator like this:

const {pr} = req.query
if(pr === 'trans' || pr === 'inst'){
    util[pr === 'trans' ? 'transUrl' : 'instaUrl'](req.originalUrl)
    .then(param => res.redirect(param))
}
else{
    res.status(400).send('Contact the editor of the originating page.')
}

Just one FYI: Please consider using === instead of == whenever you are comparing strings and there is no need of coercion.

Sign up to request clarification or add additional context in comments.

1 Comment

No actually I was wrong sorry, their variable is inst while the key is instaURL, there is an a that needs to be added by some logic, better keep the whole thing as you did then, even though they were originally checking if pr was "inst", while you are handling all non "trans" as "inst"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.