Skip to content

Instantly share code, notes, and snippets.

View oliversd's full-sized avatar

Olivers De Abreu oliversd

View GitHub Profile
@oliversd
oliversd / template-literals-multiline.js
Created January 6, 2019 18:01
Template literals multiline demo
let name='Mike';
let age=30;
let country='Italy';
console.log(`${name} is
 ${age} years old and
 lives in ${country}
`);
// Mike is
@oliversd
oliversd / template-literals.js
Created January 6, 2019 18:01
Template literals demo
let a = 5;
let b = 10;
console.log(`The sum of a and b is ${a+b} and the multiplication is ${a*b}`); 
// The sum of a and b is 15 and the multiplication is 50
let dog = {name: 'Toby', age: 3, race: 'Beagle', size: 'small'}
let puppy = {…dog, name: 'Max', age: 1}; // Clone dog object and modify its properties
console.log(puppy); // {name: 'Max', age: 1, race: 'Beagle', size: 'small'}
console.log(dog); // {name: 'Toby', age: 3, race: 'Beagle', size: 'small'}
@oliversd
oliversd / spread-operator-objects.js
Created January 6, 2019 17:56
Spread operator in objects
let name='Toby';
let age=3;
let features = {race: 'Beagle', size: 'small'};
let dog = {name, age, …features}; // We expand the features object
console.log(dog); // {name: 'Toby', age: 3, race: 'Beagle', size: 'small'}
let scores = [23, 45, 56];
function averageThreeScores(a, b, c) {
 let sum = a + b + c;
 return sum/3;
}
console.log(averageThreeScores(…scores)); // Result 41.333333…
let first = [1,2,3];
let second = [4,5,6];
// If we do this
first.push(second);
// We get
console.log(first); // [1,2,3,[4,5,6]] that is not right
// Using the spread operator
let firstName="Albert"
let lastName="Einstein"
let person = {firstName, lastName}
console.log(person.firstName); // "Albert"
console.log(person.lastName); // "Einstein"
// Old method
const myArray = ['apple', 'pear', 'orange', 'banana'];
let fruit1 = myArray[0];
let fruit2 = myArray[1];
let fruit3 = myArray[2];
let fruit4 = myArray[3];
//ES6 destructuring
let [fruit1, fruit2, fruit3, fruit4] = myArray; // much better isn't? 
//ES6 Arrow function one-liner
let doubleNumbers = [1,2,3,4,5].map(number => number*2);
//ES6 Arrow function multiple arguments
handleClick((event, seconds) => {
 event.preventDefault();
 displayFireworks();
 showAnimation(seconds);
});
//ES5
let doubleNumbers = [1,2,3,4,5].map(function(number) { 
 return number*2;
});
//ES6 Arrow function
let doubleNumbers = [1,2,3,4,5].map((number) => { 
 return number*2 
});