Back to writing
JavaScript1 min read

Using the spread operator in ES6

In ES6, the spread operator is useful for the spreading of elements of an iterable collections such as an arrays into individual function params and elements.

In the first example, lets look at getting the max value from an array of numbers. That can simply be written as:

const array = [12, 34, 19, 84, 42, 5]; const maxValue = Math.max(...array); console.log(maxValue); // 84

Alternately you may want to take a string of text and convert it to an array

const name = 'Michael'; const characters = [...name]; console.log(characters); // [M,i,c,h,a,e,l]

ES6 has allowed for cleaner, shorter code.

Thanks for reading.

More from the notebook