0

I would like to do this shorter?

function getRedditImg (img) {
  if (img === 'default') {
    return 'https://i.imgur.com/pMkc6Lo.png'
  } else if (img === 'self') {
    return 'https://i.imgur.com/pMkc6Lo.png'
  }
  return img
}

I tried doing this:

post.thumbnail === ( 'default' || 'self') ? 'https://i.imgur.com/pMkc6Lo.png' : post.thumbnail

But, it short circuits of default its true...

Any ideas? Thanks.

  • 1
    `return img === 'default' || img === 'self' ? 'https://i.imgur.com/pMkc6Lo.png' : img`? Your `( 'default' || 'self')` evaluates to `'default` because it's truthy – CertainPerformance Oct 20 '19 at 11:06
  • 1
    `return ['default', 'self'].includes(img) ? 'https://i.imgur.com/pMkc6Lo.png' : img;` – connexo Oct 20 '19 at 11:32
  • thanks @connexo, if you add it as answer ill accept it :) –  Oct 20 '19 at 11:51

1 Answers1

0

Do it witch the switch case syntax

  switch(expression) {
     case x:
        // code block
        break;
      case y:
        // code block
         break;
     default:
        // code block
   }
bill.gates
  • 14,145
  • 3
  • 19
  • 47