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

Redirecting a webpage in JavaScript

What is 'this.' keyword in JavaScript

Introduction to webshare API (navigator.share)