Search code examples
javascriptarrow-functionsdestructuring

Can I destructure an array in a Javascript arrow function?


Does any version of Javascript support destructuring of arrays in arrow functions? E.g.

const items = [ [1, 2], [3, 4] ];
const sums = items.map( [a, b] => a + b );

Solution

  • You can, but you have to surround the parameter in parentheses:

    const items = [ [1, 2], [3, 4] ];
    const sums = items.map(([a, b]) => a + b );
    console.log(sums);