<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kaiyolau on Medium]]></title>
        <description><![CDATA[Stories by Kaiyolau on Medium]]></description>
        <link>https://medium.com/@kaiyolau?source=rss-db75b6329c35------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Q8VCF4wuLrFG1CTT</url>
            <title>Stories by Kaiyolau on Medium</title>
            <link>https://medium.com/@kaiyolau?source=rss-db75b6329c35------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 13:11:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaiyolau/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use the spread operator and rest parameter]]></title>
            <link>https://medium.com/@kaiyolau/how-to-use-the-spread-operator-and-rest-parameter-65be80cc00d4?source=rss-db75b6329c35------2</link>
            <guid isPermaLink="false">https://medium.com/p/65be80cc00d4</guid>
            <category><![CDATA[rest-parameter]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[spread-operator]]></category>
            <dc:creator><![CDATA[Kaiyolau]]></dc:creator>
            <pubDate>Fri, 31 Mar 2023 17:28:52 GMT</pubDate>
            <atom:updated>2023-03-31T17:28:52.942Z</atom:updated>
            <content:encoded><![CDATA[<p>The spread operator, represented by three consecutive dots (...), is a feature introduced in ES6 (ECMAScript 2015) that allows an iterable such as an array, string, or object to be expanded into individual elements or properties.</p><p>Situation :</p><p>This is an example of using an old way to concatenate the two arrays without using the spread operator, we use the concat() method. This method takes an array as an argument and returns a new array that is the result of concatenating the original array with the argument. In this example, it looks pretty good, but think about if you want to concatenate multiple arrays, it gonna looks messy.</p><pre>const arr1 = [1, 2, 3];<br>const arr2 = [4, 5, 6];<br>const arr3 = arr1.concat(arr2);<br>console.log(arr3); // output: [1, 2, 3, 4, 5, 6]</pre><p>Here are some common use cases for the <strong>spread operator</strong>:</p><ol><li>Concatenating arrays: The spread operator can concatenate two or more arrays into a single array.</li></ol><pre>const arr1 = [1, 2, 3];<br>const arr2 = [4, 5, 6];<br>const arr3 = [...arr1, ...arr2]; // [1, 2, 3, 4, 5, 6]</pre><p>2. Copying arrays: The spread operator can be used to create a copy of an array.</p><pre>const arr1 = [1, 2, 3];<br>const arr2 = [...arr1]; // [1, 2, 3]</pre><p>3. Passing arguments to functions: The spread operator can be used to pass an array of arguments to a function.</p><pre>function sum(x, y, z) {<br>  return x + y + z;<br>}<br><br>const numbers = [1, 2, 3];<br>const result = sum(...numbers); // 6</pre><p>4. Merging objects: The spread operator can be used to merge two or more objects into a single object.</p><pre>function sum(x, y, z) {<br>  return x + y + z;<br>}<br><br>const numbers = [1, 2, 3];<br>const result = sum(...numbers); // 6</pre><p>5. Adding new elements to arrays: The spread operator can be used to add new elements to an array.</p><pre>const arr1 = [1, 2, 3];<br>const arr2 = [...arr1, 4, 5, 6]; // [1, 2, 3, 4, 5, 6]</pre><p>6. Removing elements from arrays: The spread operator can be used to remove elements from an array.</p><pre>const arr1 = [1, 2, 3, 4, 5];<br>const [, , ...arr2] = arr1; // [3, 4, 5]</pre><p>Note that the spread operator only works with iterables, which are objects that implement the iterable protocol and can be iterated over using a for…of loop or the spread operator itself. Examples of iterables in JavaScript include arrays, strings, maps, sets, and generators.</p><p>The <strong>rest parameter</strong> is a feature that was introduced in ECMAScript 6 (ES6) to allow a function to accept an arbitrary number of arguments as an array. It is represented by three dots (…) followed by the parameter name.</p><p>Here’s an example:</p><pre>typescriptCopy code<br>function sum(...numbers) {<br>  let total = 0;<br>  for (let number of numbers) {<br>    total += number;<br>  }<br>  return total;<br>}</pre><pre>console.log(sum(1, 2, 3, 4)); // output: 10</pre><p>In the above example, the <strong>sum</strong> function accepts an arbitrary number of arguments using the rest parameter <strong>...numbers</strong>. The function then loops through each number in the array and adds them up to get the total.</p><p>The rest parameter must be the last parameter in a function’s parameter list. This means that you cannot have any parameters after the rest parameter. For example, the following function definition is invalid:</p><pre>javascriptCopy code<br>function foo(x, ...rest, y) {<br>  // rest of the code<br>}</pre><ol><li>Destructuring arrays</li></ol><p>You can use the rest parameter to extract the remaining elements of an array when destructuring:</p><pre>scssCopy code<br>const [a, b, ...rest] = [1, 2, 3, 4, 5];<br>console.log(a); // 1<br>console.log(b); // 2<br>console.log(rest); // [3, 4, 5]</pre><p>In the above example, the rest parameter is used to extract the remaining elements of the array into the <strong>rest</strong> variable.</p><p><strong>Conclusion:</strong></p><p>the rest parameter allows you to pass an arbitrary number of arguments to a function and handle them as an array within the function.</p><p>Spread syntax looks exactly like rest syntax. In a way, spread syntax is the opposite of rest syntax. Spread syntax “expands” an array into its elements, while rest syntax collects multiple elements and “condenses” them into a single element.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65be80cc00d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>