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

JavaScript introduction to variables

Solutions to blocked URLs on Facebook

How to stop moz dotbot from accessing your website

How to set content disposition headers for express nodejs apps

How to set content disposition header for nginx server to force content download