Rest and Spread concepts in JavaScript and how to use these?

Rest parameters

As the name suggests rest will provide rest parameters. Let's say you have an array.

var arr = [1,2,3,4,5,6]
var [a, ...rest]= [1,2,3,4,5,6] //here rest can be any nameconsole.log(a) //1
console.log(rest) // [2,3,4,5,6]- Notice
const { id, name, username, ...restProps } = props;

Spread operator

Spread operator — Spreads the elements of an array into individual values and will give discreet properties. Let's consider this example.

var a = [1, 2];
var b = [3, 4];
var c = [...a,...b] // array a, b spread into discrete properties
console.log(c); // [1,2,3,4]

Summary:

We learned about rest and spread in this tutorial.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store