ES6 Spread and Butter in Depth (Ponyfoo)

In notebook:
Article Notes
Created at:
2016-01-05
Updated:
2016-01-05
Tags:
JavaScript Fundamentals
ES6 Spread and Butter in Depth

Rest parameters

convert a function's arguments into a real array with having to use ​apply​ or the ​arguments​ variable (​Array.prototype.slice.call(arguments)​)

so instead of:
console.log.apply(console, [1, 2, 3])
// <- '1 2 3'

you can:
console.log(...[1, 2, 3])
// <- '1 2 3'

also works with ​nodeList​ 
[...document.querySelectorAll('div')]
// <- [<div>, <div>, <div>]

concatenate two arrays:
[1, 2, ...more]

push an array into another one:
list.push(...[3, 4])

destructuring (ES6)
​[a, ...rest] = list

Default operator

​define default values for variables
  function sum (left=1, right=2) {
  return left + right
}
console.log(sum())
// <- 3
console.log(sum(2))
// <- 4
console.log(sum(1, 0))
// <- 1