DEV Community

Heru Hartanto
Heru Hartanto

Posted on

1

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more