JavaScript 中函数参数的默认参数是什么?

javascriptweb developmentfront end technology更新于 2024/7/8 14:34:00

默认参数可以轻松处理函数参数。您可以轻松设置默认参数,以允许使用默认值初始化形式参数。这仅在未传递任何值或未定义时才有可能。

示例

<html>
   <body>
      <script>
         // 默认值设置为 1
         function inc(val1, inc = 1) {
           return val1 + inc;
         }
         document.write(inc(10,10));
         document.write("<br>");
         document.write(inc(10));
      </script>
   </body>
</html>

相关文章