Es6 destructuring operator in JavaScript

JavaScript from es6 enables destructuring operation,

In JavaScript destructuring is done using the syntax;

const [elements] = [array]
const {properties} = {object}

For instance using Math object as an example destructuring can be done as follows;

Object matching syntax;

const {abs, min, max} = Math;

List matching syntax;

const [max, min, avg] = [10, 1, 5]

Fail-soft destructuring similar to object property look up returns undefined if there's no property matching;

var [item] = [];
item === undefined;

Fail-soft destructuring can also be done with predefined default vslues as follows;

var [item = 1] = [];
item === 1;

More information:

Comments

Popular posts from this blog

FlappingBird Postmortem (JS13K)

JavaScript introduction to variables

FlappingBird JS13K Games

How to target all elements of a given class name in JavaScript

Zedplug will be decommissioned on November 30th