DEV Community

loading...

Set default value in your function parameter

elukuro profile image Heru Hartanto ・1 min read

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

Discussion (0)

Forem Open with the Forem app