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
// 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 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 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 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
// Date objects in JavaScript | |
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date | |
// moment.js | |
let log = console.log; | |
let d = new Date(); | |
log(d); | |
let num = 1000 * 60 * 60 * 24 * 365.25 * 40; | |
// approx 40 yrs |
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
// Truthy and Falsy in JavaScript | |
// Boolean-ish | |
// null, undefined, 0, false, '', "", NaN - FALSEY | |
// | |
let log = console.log; | |
let es = ""; | |
let z = 0; | |
let n = null; | |
let u = undefined; |
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
//throwing Errors and Exceptions | |
//and | |
//using Try...Catch to handle thrown Errors and Exceptions | |
//Error Types: | |
// EvalError, InternalError, RangeError, ReferenceError, | |
// SyntaxError, TypeError, URIError | |
try{ | |
//throw 'Javelin'; | |
//throw {name:'Bubba', message:'Salmon'}; |
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
// template strings / template literals ES6 | |
// | |
// wrapped with back-tick character like table names in SQL | |
// Variables and expressions wrapped in ${ } | |
// Tagged Template Literals | |
const log = console.log; | |
let message = 'I\'m going to the store'; | |
let message1 = "And then he said, \"That's what she said.\""; |
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
// for..of loops | |
// for..of versus for..in loops | |
let supernatural = { | |
"actors":['Jared Padelecki', 'Jensen Ackles', 'Mark Sheppard', 'Misha Collins'], | |
"characters":['Sam Winchester', 'Dean Winchester', 'Crowley', 'Castiel'], | |
"seasons":12 }; | |
for( prop in supernatural){ | |
console.log( prop, typeof supernatural[prop], |
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
// Binary Logical Operators | |
// AND && | |
// OR || | |
// creating compound if statements | |
let ingredients = ['ham', 'onion', 'tomato']; | |
let sandwichHas = function(ingredient){ | |
for(let i of ingredients){ | |
if( i == ingredient){ |
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
// Maps vs Objects | |
// ES6 Maps are a good replacement for Objects | |
// in many circumstances but not all | |
let a = {'name': 'Sherlock'}; | |
let b = {'name': 'Watson'}; | |
let people = {}; | |
people[a] = 'Detective'; // a ['object':Object] | |
people[b] = 'Doctor'; // b ['object':Object] |