Skip to content

Instantly share code, notes, and snippets.

@dilani14
dilani14 / student-skip.js
Last active February 4, 2020 07:14
skip values in an array
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
@dilani14
dilani14 / student-default.js
Created January 29, 2020 14:37
obj destructure default
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;
@dilani14
dilani14 / student.js
Created January 29, 2020 14:11
object destructuring
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
@dilani14
dilani14 / person-nested.js
Created January 19, 2020 16:51
nested destructure
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
@dilani14
dilani14 / person-function-default.js
Created January 19, 2020 16:28
destructure function with default
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
}
@dilani14
dilani14 / person-function.js
Last active January 19, 2020 16:21
destructure in functions
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}) {
@dilani14
dilani14 / person-default.js
Created January 19, 2020 16:06
default values
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;
@dilani14
dilani14 / person-new-names.js
Last active January 19, 2020 15:47
Destructurig new names
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
@dilani14
dilani14 / person-basic.js
Created January 18, 2020 17:03
object destructuring
const person = { firstName: 'Daniel', lastName: 'Smith', age: 28 };
const { firstName, lastName, age } = person;
console.log(`Full Name: ${firstName} ${lastName}`);
//output: Full Name: Daniel Smith
@dilani14
dilani14 / person.js
Last active January 18, 2020 17:11
Person object
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