Skip to content

Instantly share code, notes, and snippets.

View prof3ssorSt3v3's full-sized avatar
🎯
Focusing

Steve Griffith prof3ssorSt3v3

🎯
Focusing
View GitHub Profile
// 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
// 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);
//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');
// 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
// 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;
//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'};
// 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.\"";
// 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],
// 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){
// 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]