Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 978 Bytes

Destructuring.md

File metadata and controls

38 lines (28 loc) · 978 Bytes

Destructuing in JavaScript

MDN documentation: The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

An example of object destructuring

// 1
const apiObject = {
  data: {
    user: 'Alex', 
    twitterHandle: 'alexpaul'
  }, 
  status: 200, 
}

// 2 
const { data } = apiObject; 

// 3
console.log(data); 
// { user: 'Alex', twitterHandle: 'alexpaul' }

/*
1. An object with various key, value pairs 
2. Using destructuring to get to only the properties we need from an object.
3. Prints out only the { data } key,value pair 
*/

/*
Exercise: use destructuring to retrieve status, add to step 2 above as we can use comma delimeters to include more properties for destructuring
*/

Resources

  1. MDN - Destructuring assignment