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
//String split and Array join methods | |
//converting Strings to Arrays and Arrays to Strings | |
//String to Array str.split(); | |
//Array to String arr.join(); | |
// | |
let sentence = "Hello my name is Inigo Montoya"; | |
let words = sentence.split(" ").sort(); | |
console.log(words); |
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
// Function Currying | |
//In JavaScript, functions are first-class objects, | |
// just like String, Number, Boolean | |
//This means that they can be passed to functions or returned from functions | |
// | |
function greet(msg){ | |
//console.log(msg); | |
return function(name){ | |
console.log(msg, name); |
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
// setTimeout | |
// How to use setTimeout, clearTimeout, setInterval | |
// and clearInterval | |
// | |
function log(msg){ | |
console.log('MESSAGE', msg); | |
clearInterval(tommy); | |
} |
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
// Callback functions | |
//built-in callback functions | |
//setTimeout, Arr.forEach, geolocation.getCurrentPosition | |
//make your own callback functions | |
//setTimeout( hello, 2000, 'Bob') | |
let names = ['Inga','Tom','Mattias','Carlos']; | |
names.forEach(hello); |
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
// Default Function Parameters | |
// new ES6 feature allowing us to provide | |
//default values for function parameters | |
function sendMessage(email, message, title="message"){ | |
if(!email || !message){ | |
return false; | |
} | |
//title = title || ""; | |
//fancy code to send the message |
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
// ES6 Destructuring | |
//extracting values from objects and arrays | |
//and assigning them to multiple variables | |
let name, id, nm, num, star, planet, rest; | |
let personObj = {name:"Arthur Dent", id:42, planet:'Earth'}; | |
let personArr = ["Zaphod", 123, 'Betelgeuse']; | |
//[nm, num] = personArr; | |
//console.log(nm, num); |
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
//Promises - Just the Basic Facts | |
//wrappers for anything async | |
// ajax calls, reading files, timeouts, geolocation, talk to a database, or anything that would use a callback function | |
//use them to get rid of callback hell | |
//fetch() returns a Promise. | |
//var result = multiplyTwoNumbers(5, 10); | |
//console.log(result); //50 | |
// | |
//var photo = downloadPhoto('http://localhost/cat.jpg'); |
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
// ES6 Promises - More syntax and examples | |
// calling resolve directly | |
// calling reject directly | |
// | |
let p1 = new Promise((resolve, reject)=>{ | |
if(true){ | |
resolve('p1 resolved'); | |
}else{ | |
reject('p1 rejected'); |
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
// Promise.all() | |
//when you only want to run your code after ALL | |
// your promises are resolved. | |
// EG: fetching remote data from multiple locations | |
let p1 = () => Promise.resolve('Got the list of users'); | |
let p2 = () => Promise.resolve('Got the list of tweets'); | |
let p3 = Promise.resolve('Got the weather'); | |
Promise.all([p1(), p2(), p3]).then((resultsArr)=>{ |
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
// Promise.race() | |
// when you only want the result from the first | |
// resolved promise | |
// | |
let p1 = Promise.reject(111); | |
let p2 = Promise.resolve(222); | |
let p3 = new Promise((resolve, reject)=>{ |