Default value for function parameter?

后端 未结 4 1928
情深已故
情深已故 2020-12-16 01:17

So I\'ve been doing this for as long as I can remember, but I\'m curious if this is really what I should be doing. You write a function that takes a parameter, so you antic

4条回答
  •  甜味超标
    2020-12-16 02:12

    In JavaScript, the argument will be undefined if the user didn't pass it in. You can use the || operator to set the value of the argument if it's undefined:

    function foo(bar) {
      bar = bar || 0;
      ...
    }
    

提交回复
热议问题