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)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay