This file contains 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
let students = ['Bob', 'John', 'Steve', 'Daniel', 'Gorge', 'Philip']; | |
const [first, , , , , last] = students; | |
console.log(`First and Last are ${first} and ${last}`); | |
//output: First and Last are Bob and Philip |
This file contains 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
let students = ['Bob', 'Jake']; | |
const [first, second, third] = students; | |
console.log(`First, Second and Thrid of the class are ${first}, ${second}, ${third}`); | |
// output: First, Second and Thrid of the class are Bob, Jake, undefined | |
const [first, second, third='Steve'] = students; |
This file contains 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
let students = ['Bob', 'James', 'Frank', 'Steve', 'Mark']; | |
// without destructuring | |
const first = students[0]; | |
const second = students[1]; | |
const third = students[2]; | |
console.log(`First, Second, Third of the class are ${first}, ${second} and ${third}`); | |
// with destructuring |
This file contains 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 = { firstName: 'Devon', lastName: 'Smith', address: { streetNo: 23, town: 'Toronto', country: 'Canada' } }; | |
const { firstName, address: {town, country} } = person; | |
console.log(`${firstName} is from ${town} ${country}`); | |
//output: Devon is from Toronto Canada |
This file contains 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 = { firstName: 'Daniel', lastName: 'Smith' }; | |
function printDetails({firstName, lastName, age: years = 28}) { | |
console.log(`${firstName} ${lastName} is ${years} years old`); | |
//output: Daniel Smith is 28 years old | |
} |
This file contains 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 = { firstName: 'Daniel', lastName: 'Smith' }; | |
// without destructuring | |
function printFullName(person) { | |
console.log(`Full name is ${person.firstName} ${person.lastName}`); | |
//output: Full name is Daniel Smith | |
} | |
// with destructuring | |
function printFullName({firstName, lastName}) { |
This file contains 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 = { firstName: 'Daniel', lastName: 'Smith' }; | |
const { firstName, lastName, age } = person; | |
console.log(`${firstName} ${lastName} is $(age) years old`); | |
//output: Daniel Smith is undefined years old | |
const { firstName, lastName, age = 28 } = person; |
This file contains 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 = { firstName: 'Daniel', lastName: 'Smith', age: 28 }; | |
const { firstName: name, lastName: surname } = person; | |
console.log(`Full Name: ${name} ${surname}`); | |
//output: Full Name: Daniel Smith |
This file contains 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 = { firstName: 'Daniel', lastName: 'Smith', age: 28 }; | |
const { firstName, lastName, age } = person; | |
console.log(`Full Name: ${firstName} ${lastName}`); | |
//output: Full Name: Daniel Smith |
This file contains 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 = { firstName: 'Daniel', lastName: 'Smith', age: 28 }; | |
const fName = person.firstName; | |
const lName = person.lastName; | |
const age = person.age; | |
console.log(`Full Name: ${fName} ${lName}`); | |
//output: Full Name: Daniel Smith |