<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: FabrizioOnorio</title>
    <description>The latest articles on DEV Community by FabrizioOnorio (@fabrizioonorio).</description>
    <link>https://dev.to/fabrizioonorio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F807434%2Fe22a9841-403c-43c8-ab7e-2718401086f1.jpeg</url>
      <title>DEV Community: FabrizioOnorio</title>
      <link>https://dev.to/fabrizioonorio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fabrizioonorio"/>
    <language>en</language>
    <item>
      <title>How to write a function that only accepts an exact number of arguments</title>
      <dc:creator>FabrizioOnorio</dc:creator>
      <pubDate>Tue, 01 Feb 2022 20:22:31 +0000</pubDate>
      <link>https://dev.to/fabrizioonorio/how-to-write-a-function-that-only-accepts-an-exact-number-of-arguments-1182</link>
      <guid>https://dev.to/fabrizioonorio/how-to-write-a-function-that-only-accepts-an-exact-number-of-arguments-1182</guid>
      <description>&lt;p&gt;How to write a function that only accepts an exact number of arguments&lt;/p&gt;

&lt;p&gt;If you find yourself having to write a function that can only accept a predefined number of arguments there are several ways to approach this using vanilla JavaScript , hereI’ll describe 2 simple ways of doing this, using the &lt;em&gt;arguments object&lt;/em&gt; and &lt;em&gt;rest parameters&lt;/em&gt; (this last one is preferred for ES6 and arrow functions).&lt;/p&gt;

&lt;h2&gt;
  
  
  The arguments object:
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;arguments&lt;/em&gt; is an array object accessible inside functions that contains the values of the arguments passed to that function as explained on &lt;a href="https://developer.mozilla.org/"&gt;https://developer.mozilla.org&lt;/a&gt;/. &lt;/p&gt;

&lt;p&gt;Let's see this on use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b, c) {

      return arguments[0] + arguments[1] + arguments[2];

    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the same thing as doing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b, c) {

      return a + b + c;

    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, if we want to accept only 3 arguments, no more, no less we could do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(a, b, c) {

      if (arguments.length === 3) {

        return a + b + c;

      }

    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rest parameters:
&lt;/h2&gt;

&lt;p&gt;The rest parameter allows a function to accept an indefinite number, or in our case defined number of arguments as an array in JavaScript.&lt;/p&gt;

&lt;p&gt;Let’s achieve the same as before but with the rest parameters this time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(...args) {

      if (args.length === 3) {

        return args[0] + args[1] + args[2];

      }

    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case &lt;em&gt;args&lt;/em&gt; represents the array we access our values from. In order to be accessed it is essential to use the ‘…’. Furthermore, this should be the way to go in arrow functions as previously explained.&lt;/p&gt;

&lt;p&gt;I hope this was useful.&lt;/p&gt;

&lt;p&gt;Happy coding 🚀&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
