What is “Parameters without defaults after default parameters in JavaScript”

JavascriptWeb DevelopmentFront End Technology

The default parameter came to handle function parameters with ease. Default parameters allow you to initialize formal parameters with default values. This is possible only if no value or undefined is passed. With ES6, you can easily set default parameters. Let’s see an example

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>

The following code runs correctly showing the working of parameters from left-to-right. It overwrites default parameters even if parameters are added after without default.

Example

Live Demo

<html>
   <body>
      <script>
         function display(val1 = 10, val2) {
            return [val1, val2];
         }
         document.write(display());
         document.write(display(20));
      </script>
   </body>
</html>
raja
Published on 24-Jan-2018 11:48:01
Advertisements