JavaScript 函数中的可选参数是什么?


要想在 JavaScript 中声明可选函数参数,请使用“default”参数。

示例

你可以尝试运行以下代码来声明可选参数 -

<html>
   <body>
      <script>
         // default is set to 1
         function inc(val1, inc = 1) {
            return val1 + inc;
         }
         document.write(inc(10,10));
         document.write("<br>");
         document.write(inc(10));
      </script>
   </body>
</html>

更新日期:2020-06-22

196 次浏览

开启您的 职业生涯

完成课程获得认证

开始吧
广告
© . All rights reserved.