Posts

Showing posts with the label Spread

REST parameters and spread operator in ES-6

Code for this and several other ES-6 features can be found in my  github In ES-5 if a function has to take an arbitrary number or parameters, the exact count of which is decided only during run time, the only option was to construct an array and pass it as a parameter. In ES-6 a simpler and more expressive notation has been introduced called the  REST parameters, which are represented using the spread operator. Syntax for the spread operator is to prefix the variable name with three periods. For example,  REST  parameter “a” is represented using the spread operator as follows: ...a These parameters are ultimately converted into an array by the spread operator, there by taking the array creation responsibility away from the developer. Let us consider a few use cases for these new operators. CASE  1: Passing arbitrary number of parameters. ES-5: var add = function(a,b){ console.log(a+(b.reduce((x,y)=>{return x+y}))); ...