Getting Started with Modern JavaScript — Destructuring

Destructuring Arrays

const [apple, banana, kiwi] = fruit;
const [,,kiwi] = ['apple', 'banana', 'kiwi'];
const [apple, …rest] = ['apple', 'banana', 'kiwi'];console.log(rest); // -> ['banana', 'kiwi']

Destructuring Objects

const rectangle = { width: 5, height: 8 };const { width } = rectangle;
const { width, height } = rectangle;
const { width: w, height: h } = rectangle;console.log(w); // -> 5

Default Values

const { password } = {};console.log(password); // -> undefined

Examples

String Manipulation

const [firstName, lastName] = 'Max Best'.split(' ');// firstName = 'Max', lastName = 'Best'

Swapping Variable Values

[me, you] = [you, me];// me = 'happy', you = 'sad'

Function Parameter Objects

Multiple Return Values

Conclusion

Getting Started Series

Angular by day, React by night. Senior Consultant jProfessionals. Educator wannabe. ngVikings organizer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store