
Spread vs Rest Operator In JavaScript
It is very confusing to use spread and rest operator in javaScript because both have same syntax (...) but working is totally different . Let's see the difference between them. Spread Operator : The spread operator is used to expand or spread individual values of an array or properties of an object. Code Example : const arr = [ 1 , 2 ]; console . log (... arr ); // 1 2 It takes [1, 2] and spread values of array and return 1 2 . Function Example : with spread operator function add ( a , b ){ return a + b ; } const arr = [ 3 , 4 ]; console . log ( add (... arr )); // 3 ,4 we spread the values of array. Same function without spread operator function add ( a , b ) { return a + b ; } const arr = [ 3 , 4 ]; console . log ( add ( arr [ 0 ], arr [ 1 ])); // 7 Rest Operator : It is opposite of spread operator. It collect all remaining properties from an object or array in a new array or object. with rest operator function sum (... nums ) { console . log ( nums ); } sum ( 1 , 2 , 3 ); // [1, 2,
Continue reading on Dev.to Webdev
Opens in a new tab




