bradxr
8/4/2018 - 2:38 PM

Arrow Functions

Create functions without using the function keyword, and sometimes without the return keyword

// standard function
const lordify = function( firstName ) {
  return `${firstName} of Canterbury`
};

// arrow function syntax
const lordify = ( firstName ) => `${firstName} of Canterbury`

// if the function only takes one argument, like above, the parentheses can be removed
const lordify = firstName => `${firstName} of Canterbury`

// a function with more than one line needs to be surrounded with brackets
const lordify = ( firstName, land ) => {
  
  if( !firstName ) {
    throw new Error( 'A firstName is required to lordify' )
  }
  
  if( !land ) {
    throw new Error( 'A lord must have land' )
  }
  
  return `${firstName} of ${land}`
}