How can I declare optional function parameters in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To declare optional function parameters in JavaScript, use the “default” arguments. You can try to run the following code to declare optional parameters

Example

Live Demo

<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>
raja
Published on 29-Jan-2018 10:17:38
Advertisements