DEV Community

Heru Hartanto
Heru Hartanto

Posted on

Set default value in your function parameter

Hi Dev Community 😇

Did you know that we can actually set our function parameter with a default value, It will make your function at least return default value instead of undefined, sound great isn't it 🤩

    function multiply(a=1 ,b = 1) {
        return a * b;
    }
    console.warn(multiply(3,2))
    // expected output : 6 (3 multiply by 2)

    console.warn(multiply(5))
    // expected output: 5 ( 5 multiply by 1)

    console.warn(multiply())
    // expected output: 1 (1 multiply by 1)
Enter fullscreen mode Exit fullscreen mode

now could you predict the expected output from this function 🤔

function substraction(a=1 ,b = 3) {
        return a - b;
    }
console.warn(substraction(3))
// the result is ?
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