Created
July 21, 2017 17:37
-
-
Save fresh5447/c32d42e9d939246fa7ae5c260fc4a2c7 to your computer and use it in GitHub Desktop.
Quick overview of arrow functions
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
| // ES5 Function | |
| function sayHi(name){ | |
| return "Hello, there " + name + "!"; | |
| } | |
| // console.log(sayHi("Doug")); // "Hello, there Doug!" | |
| // new var | |
| // ES6 Arrow Function | |
| const sayHello = (name) => { | |
| return "Hello, there " + name + "!"; | |
| } | |
| // example using string interpolation | |
| const sayHello2 = (name, age) => { | |
| return `Hello, there ${name}, whats your ${age}!`; | |
| } | |
| // console.log(sayHello2("Doug", 18)); | |
| // const sayHello3 = (name) => `Hello, there ${name}` | |
| const sayHello3 = name => `Hello, there ${name}` | |
| // console.log(sayHello3("Doug")) | |
| // function getSum(a,b) { | |
| // return a + b | |
| // } | |
| // | |
| // const getSum = (a,b) => a + b; | |
| const getSum = (a,b) => { | |
| return a + b; | |
| } | |
| // console.log(getSum(3,6)); | |
| const numbers = [1,3,5,6,8,9,10]; | |
| // map through and return an array of objects, with count property. | |
| var mappedNumbers = numbers.map(function(num){ | |
| return {count: num} | |
| }); | |
| const mappedNumbers2 = numbers.map((num) => { | |
| return {count: num} | |
| }); | |
| const stillNums = numbers.map(num => num); | |
| // filter and return nums greater than 6 using an arrow function. | |
| const filteredNums = numbers.filter((n) => n > 6); | |
| const filteredNums = numbers.filter((n) => { | |
| return n > 6 | |
| }); | |
| console.log(filteredNums); | |
| // Objects | |
| // ES5 | |
| var name = "Doug"; | |
| var age = 22; | |
| var person = { name: name, age: age }; | |
| // ES6 | |
| const nombre = "Doug"; | |
| const anons = 22; | |
| const persono = { nombre, anons }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment