javascriptecmascript-6default-parameters

It is possible to use default parameter value its same function name


I would like to know if it is possible to use the function itself in its default parameter.

function somename(a,b=somename()){
    return a+b;
}
somename(10);

Solution

  • Sure you can, as long as you design the logic such that it doesn't enter an endless recursive loop. For example:

    function somename(a, b = somename(3, 5)) {
      return a + b;
    }
    console.log(somename(10));