Deanna Tran 338d7ee8a7 Change "rest operator" to say "rest parameter" in challenges and guide (#35496)
* change es6 challenges rest operator to param

* fix rest parameter typos

* change rest operator to parameter in guide

* fix casing

* change rest operator to rest parameter in guide

* change rest operator to rest parameter in curriculum

* remove extra whitespace

* remove whitespaces

* remove whitespace

* fix: removed arabic file

* fix: removed chinese file
2019-05-08 16:30:24 +02:00

1.8 KiB

title
title
Use the Spread Operator to Evaluate Arrays In-Place

Use the Spread Operator to Evaluate Arrays In-Place

Spread Operator explained

Mozilla Developer Network Spread Operator

Spread Operator compared to Rest Parameter

Stack Overflow

Video Explaining Spread Operator and Rest Parameter

Image of youtube video link spread and rest parameter

Information About apply() Method

Mozilla Developer Network Apply Method

3 Quick Examples

let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min.apply(null, numbers);
console.log(minNum);//-12
let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min(numbers);
console.log(minNum);//NaN 
let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min(...numbers);
console.log(minNum);//-12

SPOILER WARNING: SOLUTION AHEAD

The Solution

Unpacking the arr1 using the spread operator and then copying those values to arr2

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
  "use strict";
  arr2 = [...arr1]; // change this line
})();
console.log(arr2);