<?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: Devendra Gaud</title>
    <description>The latest articles on DEV Community by Devendra Gaud (@devendra0110).</description>
    <link>https://dev.to/devendra0110</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%2F216466%2Fc91e5122-e2a3-428d-991e-3f55f786f227.png</url>
      <title>DEV Community: Devendra Gaud</title>
      <link>https://dev.to/devendra0110</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devendra0110"/>
    <language>en</language>
    <item>
      <title>arguments before rest operator 😲</title>
      <dc:creator>Devendra Gaud</dc:creator>
      <pubDate>Fri, 13 Dec 2024 09:22:56 +0000</pubDate>
      <link>https://dev.to/devendra0110/arguments-before-rest-operator-3k8h</link>
      <guid>https://dev.to/devendra0110/arguments-before-rest-operator-3k8h</guid>
      <description>&lt;p&gt;Alert !!! First Post &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/pYlE39do2VbOedaQgg/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/pYlE39do2VbOedaQgg/giphy.gif" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Did you ever think about what was the world(javascript) before the rest operator? Yes, I'm talking about the (...) introduced in ES6.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiplyBy2(...values){
    for(let i=0;i&amp;lt;values.length;i++){
        console.log(2*values[i]);
    }
  }

  multiplyBy2(10,20,30,40,50)

// Output
20
40
60
80
100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you ever think about 🤔 what technique was used before to create variadic functions?&lt;/p&gt;

&lt;p&gt;One possible solution which comes to our(mine) mind first is that, instead passing of values, we pass an array directly. Obviously nothing wrong with that.&lt;/p&gt;

&lt;p&gt;But there's the story of a saviour, which you might not have heard about. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx95lu7pj05wzaopo4my1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx95lu7pj05wzaopo4my1.png" alt="arguments word art" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;arguments is an array-like object accessible inside functions that contains the values of the arguments passed to that function. &lt;br&gt;
MDN&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple words, it was used to create the functions where you need to pass the arguments more than the function was supposed to accept.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsed6emc7h4iijqvhh0nb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsed6emc7h4iijqvhh0nb.png" alt="Talk is cheap show me the code" width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's understand more about it.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiply(a,b){
    return arguments[0] * arguments[1]
}
console.log(multiply(4,5))

// output 
20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nice. But, so what you just replace the a and b with "arguments". Let's extend the example a bit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiply(a,b){
    return arguments[0] * arguments[1] * arguments[2]
}
console.log(multiply(4,5,6))

// output 
20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you are using the &lt;code&gt;arguments&lt;/code&gt; object to access the passed value, you do not need to declare parameters in function definition &lt;code&gt;a, b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;⚠️ &lt;code&gt;arguments[0]&lt;/code&gt; seems like we are accessing an array using index but we're not. We(MDN) already said it's an &lt;code&gt;array-like object&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
function multiply(){
    let result = 1;
    //since arguments not an array but an object
    let values = Object.values(arguments);
    for(let i = 0; i &amp;lt; values.length; i++){
        result *= values[i];
    }
    return result;
}
console.log(multiply(4,5,6));

Output 
20

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/fs6rnt1K0YyVWRXNwY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/fs6rnt1K0YyVWRXNwY/giphy.gif" width="478" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right!&lt;/p&gt;

&lt;h2&gt;
  
  
  What's the issue then
&lt;/h2&gt;

&lt;p&gt;But you might not have seen it often. The problem arises when we use it with arrow functions. The &lt;code&gt;arguments&lt;/code&gt; object is the built-in part of the &lt;code&gt;function&lt;/code&gt;. So, it's not accessible in the arrow functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multiply = (a,b) =&amp;gt; {
    return arguments[0] * arguments[1]
}
console.log(multiply(4,5))

// Output
Uncaught ReferenceError: arguments is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: In modern code, rest parameters should be preferred&lt;br&gt;
MDN&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;arguments&lt;/code&gt; object give you access to the arguments passed to the function&lt;/li&gt;
&lt;li&gt;Unaccessible inside arrow functions&lt;/li&gt;
&lt;li&gt;Use rest parameters when creating variadic =&amp;gt; functions.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>arguments</category>
      <category>restoperator</category>
    </item>
  </channel>
</rss>
