Jon Rumsey

An online markdown blog and knowledge repository.


Project maintained by nojronatron Hosted on GitHub Pages — Theme by mattgraham

ES6 Arrow Functions

ECMAScript 2015 aka ES6 Arrow Functions notes taken while reading MDN and caniuse.com

MDN Docs on Arrow Functions

MDN Docs: Arrow Function Expressions

Function Expression

Recall that the function keyword defines a Function Expression.
The syntax is: function [name]([param1[,paramN]]) { statements; }; [Mozilla Developer Network Documentation]

Name is the plain-english (or complicated gibberish) word by which the function can be referenced.
Parameters represent arguments to be passed in to the function.
There can be zero or one or many parameters.
A Code Block of statement is executed when the function is 'called'.
Functions always return something:

Arrow Functions

A compact alternative to function expressions.

Not-so-good Aspects:

About using 'this' keyword with functions and arrow functions:

How To Construct Arrow Functions

param => expression

Note the following:

let myWord = 'bar';

function foo(word){
  return `FOO ${word}.`;
}

let bar = word => `FOO-FOO ${word}.`;

bar (myWord);
'FOO-FOO bar.'

Transform a JS Function to an Arrow Function

ES 6.0 defines a function as a variable that uses the keyword 'function', followed by parentheses for a parameters list, followed by a code block.

Arrow Functions:

Example:

function randomNumber() {
  return Math.random;
}

...can be rewritten as an arrow function:

randomNum = () => Math.random;

Parens and Params

Parenthesis are required to contain the input parameters, or no parameters at all.

(parm1, parm2) => expression
() => expression

Braces and Code Blocks

Braces are not required for single statement expressions that do not overflow to the next line.

Multi-line statements require:

() => {
  let expression1 = expression;
  return expression1;
}
param => {
  let result = expression + param;
  return result;
}

Return an Object Literal

Straight from [MDN, 'Arrow_functions']:

params => ({foo: "a"}) // returns object {foo: 'a'}

Object Literals have many uses, including:

Advanced Uses

Arrow Functions are useful for many purposes including:

An example Event Handler registration using an Arrow Function:

// JS Function method:
document.addEventListener('click', function() {
  console.log('Click');
})

// Arrow Function method:
document.addEventListener('click', () => console.log('Click'));

Neat!

Summarized Configurations

Arrow Functions can be written in several different ways, with options on when to use parenthesis and braces.

Parentheses:

/* zero parameters requires parens */
const alpha = () => `Hello World!`;

/* multiple params requires parens */
const bravo = (name, age) => `${name} is ${age} years old.`;

/* single param does not require parens */
const charlie = name => `Her name is ${name}.`;

/* surround object literal with parens */
const delta = (name, age) => ({
  name: name,
  age: age,
});

Braces:

/* single code statement does not require braces */
let echo = () => `Hello Hello World World!`;

/* multi-line code block requires braces */
let foxtrot = (length, width) => {
  let ttlLen = length * 2;
  let ttlWid = width * 2;
  let perimeter = ttlLen + ttlWid;
  return `Perimeter: ${perimeter}`;
}

Caniuse.com Summary Notes

Support for ES6 Arrow Functions is pretty vast, even on mobile and specialty platforms.

Note: IE (of course) and Opera Mini are two outliers as they do not support arrow functions.

References

caniuse.com

MDN Arrow Functions

WebDevSimplified ES6 Arrow Functions Tutorial on Youtube

Back to readme.md