1const HIGH_TEMPERATURES = {
2 yesterday: 75,
3 today: 77,
4 tomorrow: 80
5};
6
7//ES6 assignment syntax
8const {today, tomorrow} = HIGH_TEMPERATURES;
9
10//ES5 assignment syntax
11const today = HIGH_TEMPERATURES.today;
12const tomorrow = HIGH_TEMPERATURES.tomorrow;
13
14console.log(today); // 77
15console.log(tomorrow); // 80
16console.log(yesterday); // "ReferenceError: yesterday is not defined" as it should be.
1({ a, b } = { a: 10, b: 20 });
2console.log(a); // 10
3console.log(b); // 20
4
5
6// Stage 4(finished) proposal
7({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
8console.log(a); // 10
9console.log(b); // 20
10console.log(rest); // {c: 30, d: 40}
11
1//Without destructuring
2
3const myArray = ["a", "b", "c"];
4
5const x = myArray[0];
6const y = myArray[1];
7
8console.log(x, y); // "a" "b"
9
10//With destructuring
11
12const myArray = ["a", "b", "c"];
13
14const [x, y] = myArray; // That's it !
15
16console.log(x, y); // "a" "b"
1function f() {
2 return [1, 2];
3}
4
5let a, b;
6[a, b] = f();
7console.log(a); // 1
8console.log(b); // 2
9
1/*
2* On the left-hand side, you decide which values to unpack from the right-hand
3* side source variable.
4*
5* This was introduced in ES6
6*/
7const x = [1, 2, 3, 4, 5]
8const [a, b] = x
9console.log(a) // prints out: 1
10console.log(b) // prints out: 2