es6 define function code example

Example 1: Arrow Functions

// The usual way of writing function
const magic = function() {
  return new Date();
};

// Arrow function syntax is used to rewrite the function
const magic = () => {
  return new Date();
};
//or
const magic = () => new Date();

Example 2: es6 arrow function

//ES5
var phraseSplitterEs5 = function phraseSplitter(phrase) {
  return phrase.split(' ');
};

//ES6
const phraseSplitterEs6 = phrase => phrase.split(" ");

console.log(phraseSplitterEs6("ES6 Awesomeness"));  // ["ES6", "Awesomeness"]

Example 3: concise body arrow functions javascript

const plantNeedsWater = day => day === 'Wednesday' ? true : false;

//If only 1 Parameter no () needed
//Single line return is implicit
//Single line no {} needed