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 name='Mike'; | |
let age=30; | |
let country='Italy'; | |
console.log(`${name} is | |
${age} years old and | |
lives in ${country} | |
`); | |
// Mike is |
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 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 |
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 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'} |
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 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'} |
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 scores = [23, 45, 56]; | |
function averageThreeScores(a, b, c) { | |
let sum = a + b + c; | |
return sum/3; | |
} | |
console.log(averageThreeScores(…scores)); // Result 41.333333… |
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 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 |
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 firstName="Albert" | |
let lastName="Einstein" | |
let person = {firstName, lastName} | |
console.log(person.firstName); // "Albert" | |
console.log(person.lastName); // "Einstein" |
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
// 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? |
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
//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); | |
}); |
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
//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 | |
}); |
NewerOlder