Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Difference Between the Spread and Rest Operators in JavaScript

// We use the spread operator to spread array values or iterables into maybe an array or object.

// While we use the Rest operator to gather the remaining elements passed into a function as an array.

const myFunction = (name1, ...rest) => { // used rest operator here
    console.log(name1);
    console.log(rest);
};

let names = ["John", "Jane", "John", "Joe", "Joel"];
myFunction(...names); // used spread operator here
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: formidable node js 
Javascript :: export default module 
Javascript :: yt playlist downloader js 
Javascript :: LocomotiveScroll npm 
Javascript :: Highest and Lowest 
Javascript :: JavaScript max 32-bit integer 
Javascript :: jest test thunk 
Javascript :: {{i | json}} 
Javascript :: js if the reverse of a number is better than the original num 
Javascript :: javascript while loops 
Javascript :: repeat network call n times in js 
Javascript :: json with postgresql 
Javascript :: linear search algorithm in javascript 
Javascript :: discord.js find word inside comment 
Javascript :: res.write image url 
Javascript :: snackbar in react 
Javascript :: fetch is not defined jest react 
Javascript :: how to disable eval in javascript 
Javascript :: shopify get list of all products ajax api 
Javascript :: cant see serviceWorker.js 
Javascript :: JS function typeof 
Javascript :: clean my react app 
Javascript :: how to check url with port is valid or not regex javascript 
Javascript :: obtain only integer not decimal js 
Javascript :: react hook form with controlled input 
Javascript :: json schema bsp 
Javascript :: axios.get Uncaught (in promise) TypeError: response.json is not a function 
Javascript :: Fake Binary 
Javascript :: jquery get table 
Javascript :: when click play sound 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =