-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy path07-destructuring.ts
More file actions
32 lines (22 loc) · 937 Bytes
/
07-destructuring.ts
File metadata and controls
32 lines (22 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Destructuring refers to the ability to take an object, or array, and directly assign properties to variables.
// This is actually a pretty handy little feature that you don't realise you need until you use it.
// TypeScript, of course, supports types through this syntax as well.
const data = {
fullName: 'Blake Embrey',
email: 'hello@blakeembrey.com',
luckyNumber: 3
}
// Object destructuring. Pick the properties you want.
const { luckyNumber } = data
console.log(luckyNumber)
const arrayData = [154, 'Blake', true]
// Array destructuring. Pick the elements you want.
const [,me] = arrayData
console.log(me)
// Object and array destructuring makes it easier to return multiple pieces of data back to the user too!
function lookup (name: string) {
const username = 'blakeembrey'
const email = 'hello@blakeembrey.com'
return [username, email]
}
// However, an object might be clearer here, so judgement is wise.