Last active
July 24, 2020 13:40
-
-
Save gabrielecanepa/b815bc052ff2fb22767bed671cef5ded to your computer and use it in GitHub Desktop.
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
// 1. null vs. undefined | |
// null is a value, representing "nothing" | |
// undefined means that the variable was never assigned | |
const nullValue = null | |
const undefinedValue = undefined | |
let name // I declare this variable, but I don't assign it | |
console.log(name) // => undefined | |
// 2. const vs. let | |
// const CAN'T be reassigned | |
const myName = 'Gabriele' // declaration | |
// myName = 'Gabi' // error -> assignment to constant variable | |
// let CAN be reassigned | |
let nickname = 'Gabi' | |
nickname = 'Gabri' | |
// A constant can be changes/manipulated though! | |
const friends = ['Jack', 'Edo'] | |
friends.push('Federico') // this is NOT a reassignment! I'm manipulating the object without assigning it again | |
// 3. Looping | |
// forEach is NOT modifying and giving me back another array, it's just a 'general' iteration. Inside this iteration I can do different things | |
// forEach always returns undefined | |
const myFriends = friends.forEach(friend => { | |
console.log(friend.toLowerCase()) | |
}) | |
// map is acting on the array and giving you back a new array! | |
// the return of map is a new array and not undefined | |
const myNewFriends = friends.map(friend => friend.toLowerCase()) // one line arrow function, the ONLY case where the return is implicit | |
console.log(myNewFriends) | |
// "mapping" with a forEach (try to avoid and use reduce!) | |
const lowercasedFriends = [] | |
friends.forEach(friend => lowercasedFriends.push(friend.toLowerCase())) | |
console.log(lowercasedFriends) | |
// reduce creates a new value through an iteration | |
// The first parameter is the new value I want to fill with the elements coming from the iteration | |
// The second parameter is the current element of the iteration | |
// After the function I pass the value of the initial element, usually 0/empty array/empty object | |
// Filling an empty array (instead of the forEach seen before) | |
const friendsLength = friends.reduce((lengths, friend) => { | |
lengths.push(friend.length) | |
return lengths // remember the return! | |
}, []) | |
console.log(friendsLength) | |
// "Filling" a number starting from 0 | |
const totalLength = friends.reduce((length, friend) => { | |
length += friend.length | |
return length | |
}, 0) | |
console.log(totalLength) | |
// Filling an empty object | |
const friendsObject = friends.reduce((friendsWithLength, friend) => { | |
friendsWithLength[friend.toLowerCase()] = friend.length | |
return friendsWithLength | |
}, {}) | |
console.log(friendsObject) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment