This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function countryInfo(country) { | |
| const name = country.name; | |
| const region = country.region || 'the world'; | |
| const code2 = country.codes.cca2; | |
| const dialcode = country.codes.dialcode; | |
| const cities = country.cities; | |
| return ( | |
| ` | |
| COUNTRY TIPS: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // OLD METHOD | |
| const name = country.name; | |
| const region = country.region || 'the world'; | |
| const code2 = country.codes.cca2; | |
| const dialcode = country.codes.dialcode; | |
| const cities = country.cities; | |
| // ES6 DESTRUCTURING | |
| const { | |
| name, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const person = { | |
| name: 'Glad Chinda', | |
| birthday: 'August 15' | |
| } | |
| // FUNCTION WITHOUT DESTRUCTURED PARAMETERS | |
| function aboutPerson(person = {}) { | |
| const { name, birthday, age = 'just a few' } = person; | |
| console.log(`My name is ${name} and I'm ${age} years old. I celebrate my birthday on ${birthday} every year.`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Array Destructuring | |
| const [red, green, blue] = color; | |
| console.log(`R: ${red}, G: ${green}, B: ${blue}`); | |
| // R: 240, G: 80, B: 124 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const [,, blue] = color; | |
| console.log(`B: ${blue}`); | |
| // B: 124 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fetchDatabaseRecords(fromDate, toDate) { | |
| if (fromDate > toDate) { | |
| // swap the dates using array destructuring | |
| [fromDate, toDate] = [toDate, fromDate]; | |
| } | |
| // ...execute database query | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The Rectangle constructor | |
| function Rectangle(length, breadth) { | |
| this.length = length || 10; | |
| this.breadth = breadth || 10; | |
| } | |
| // An instance method | |
| Rectangle.prototype.computeArea = function() { | |
| return this.length * this.breadth; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rectangle { | |
| // The class constructor | |
| constructor(length, breadth) { | |
| this.length = length || 10; | |
| this.breadth = breadth || 10; | |
| } | |
| // An instance method | |
| computeArea() { | |
| return this.length * this.breadth; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // An anonymous class expression | |
| // assigned to a variable | |
| const Rectangle = class { | |
| // The class constructor | |
| constructor(length, breadth) { | |
| this.length = length || 10; | |
| this.breadth = breadth || 10; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Rectangle { | |
| constructor(length, breadth) { | |
| this.length = length || 10; | |
| this.breadth = breadth || 10; | |
| } | |
| computeArea() { | |
| return this.length * this.breadth; | |
| } | |
| } |