Answers for "javascript rest parameters vs spread operator"

3

javascript rest parameters vs spread operator

/** 
* JS Spread and Rest operators:
* Two operators with the same syntax (...) but behave differently
*/

// Rest parameter: collects all remaining elements into an array. 
function foo (...args) { console.log(args) } 
foo(1,2,3,4,5,6) // Output: (6) [1,2,3,4,5,6]

// Spread operator: allows iterables to be expanded into single elements.
let arr = [1, 2, 3];
let arrCopy = [-1, 0, ...arr]; // spread the array into a list of parameters
                        // then put the result into a new array
Posted by: Guest on May-23-2021
-2

spread and rest operator javascript

function myData(...args){console.log(args) ; // ["Marina",24,"Front-End Developer"]}myData("Marina",24,"Front-End Developer") ;
Posted by: Guest on June-25-2020

Code answers related to "javascript rest parameters vs spread operator"

Code answers related to "Javascript"

Browse Popular Code Answers by Language