DEV Community

Heru Hartanto
Heru Hartanto

Posted on

Shorthand for calling functions conditionally

You actually can create a shorthand for calling functions conditionally, and I just found out this yesterday 😭

const type = 'greeting'; function greeting() { console.log('hello') } function goodbye() { console.log('goodbye') } if(type === 'greeting'){ greeting() }else{ goodbye() } 
Enter fullscreen mode Exit fullscreen mode

Instead of doing that, we can reduce our line number this way

(type==='greeting' ? greeting:goodbye)() 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)