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)