Skip to content

Instantly share code, notes, and snippets.

// #1 ES6: if passing one argument you don't need to include parenthesis around parameter.
var kitty = name => name;
// same as ES5:
var kitty = function(name) {
return name;
};
// #2 ES6: no parameters example.
var add = () => 3 + 2;
// Line breaks are not allowed and will throw a syntax error
let func1 = (x, y)
=> {
return x + y;
}; // SyntaxError
// But line breaks inside of a parameter definition is ok
let func6 = (
x,
y
// #1
// Before:
function sayHi(petSquirrelName) { console.log('Greetings ' + petSquirrelName + '!'); }
sayHi('Brigadier Sir Nutkins II'); // => Greetings Brigadier Sir Nutkins II!
// After:
function sayHi(petSquirrelName) { console.log(`Greetings ${petSquirrelName}!`); }
sayHi('Brigadier Sir Nutkins II'); // => Greetings Brigadier Sir Nutkins II!
// #2
function bunnyBailMoneyReceipt(bunnyName, bailoutCash) {
var bunnyTip = 100;
console.log(
`
Greetings ${bunnyName.toUpperCase()}, you have been bailed out!
Total: $${bailoutCash}
Tip: $${bunnyTip}
------------
var squirrelNames = ['Lady Nutkins', 'Squirrely McSquirrel', 'Sergeant Squirrelbottom'];
var bunnyNames = ['Lady FluffButt', 'Brigadier Giant'];
var animalNames = ['Lady Butt', squirrelNames, 'Juicy Biscuiteer', bunnyNames];
animalNames;
// => ['Lady Butt', ['Lady Nutkins', 'Squirrely McSquirrel', 'Sergeant Squirrelbottom'], 'Juicy Biscuiteer', ['Lady FluffButt', 'Brigadier Giant']]
// To flatten this array we need another step:
var flattened = [].concat.apply([], animalNames);
flattened;
var squirrelNames = ['Lady Nutkins', 'Squirrely McSquirrel', 'Sergeant Squirrelbottom'];
var bunnyNames = ['Lady FluffButt', 'Brigadier Giant'];
var animalNames = ['Lady Butt', ...squirrelNames, 'Juicy Biscuiteer', ...bunnyNames];
animalNames;
// => ['Lady Butt', 'Lady Nutkins', 'Squirrely McSquirrel', 'Sergeant Squirrelbottom', 'Juicy Biscuiteer', 'Lady FluffButt', 'Brigadier Giant']
var values = [25, 50, 75, 100]
// This:
console.log(Math.max(25, 50, 75, 100)); // => 100
// Is the same as this:
console.log(Math.max(...values)); // => 100
/*
NOTE: Math.max() typically does not work for arrays unless you write it like:
function myFunction(x, y, z) {
console.log(x + y + z)
};
var args = [0, 1, 2];
myFunction.apply(null, args);
// => 3
function myFunction(x, y, z) {
console.log(x + y + z);
}
var args = [0, 1, 2];
myFunction(...args);
// => 3
var dateFields = readDateFields(database);
var d = new Date(…dateFields);