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

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript