JavaScript Spread Operator

What is the JavaScript spread operator? How do you use it? Why would you use something that looks so ridiculous? This post will hopefully answer these questions.

The spread operator, in simple terms, is a way to "spread" the contents of an array or object

The JavaScript spread operator is a way to expand an array or list into a concatenable type. This will make more sense after looking at a few examples and code snippets. If you do not understand arrays or lists within JavaScript please read this post first - Arrays and Lists in JavaScript

If we want to combine two arrays in JavaScript we can use the Concat function, which stands for concatenation. This function does not affect the arrays, it returns a new array with the combination of the two arrays. The 'result' variable in the code snippet below will contain an array with the number 1 through 10 inside.

   
    <script>

        var propOne = [10,20,30,40];

        var propTwo = [99,79];

        var totalProps = [...propOne, ...propTwo];

        console.log(totalProps);

    </script>

    JavaScript Rest Operator
     * u can pass or collect the number of arguments in     function is called as a REST Operator.
    <script>

        function abc(...numbers){
            console.log(numbers)
        }
   
        abc("suresh","ramesh","kishore");
        abc(99,89,79,449,11259);

    </script>

    and you can devide the parameters, as a seperate values.     
    <script>

        function abc(a,b,...numbers){
            console.log(a)
            console.log(b)
            console.log(numbers)
        }
   
        // abc("suresh","ramesh","kishore");
        abc(99,89,79,449,11259);

    </script>