ES6 Arrow Functions

node v4.9.1
version: 2.0.0
Arrow functions (also known as fat arrow functions) have a shorter syntax compared to traditional function expressions and lexically bind the this value. Arrow functions are also always anonymous. Here we are using the shortest form, where the body is a simple expression and we can drop the parenthesis around the parameter since there is only one:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Same as function(number) { return number * 2 } var doubled = => number * 2);
If we want multiple parameters, we can add the parenthesis and curly braces back:
// Same as function(runningTotal, number) { return runningTotal + number } var sum = numbers.reduce((runningTotal, number) => runningTotal + number, 0);
Finally, more complex multistatement arrow functions can use curly braces:
// Here all we're really saving is the function() heading. => { if (number % 3 == 0 && number % 5 === 0) return "Fizz Buzz"; if (number % 3 === 0) return "Fizz"; if (number % 5 === 0) return "Buzz"; return number; }).join("<br/>")
Arrow functions can also make your code less complex since they'll use the "this" of the surrounding scope, instead of introducing their own. This means you can avoid having to bind in many cases:
var cart = { items: [], addItem: function(anItem) { console.log("Added " + anItem + "!"); this.items.push(anItem); }, addAll: function(newItems) { // Before we'd have to do (function(anItem) { this.addItem(anItem) }).bind(this) newItems.forEach(anItem => this.addItem(anItem)); } } cart.addAll(["pickels", "tomatoes", "cheese"])
To learn more about arrow functions, check out Some of these examples and explanations originally taken and modified from:


  • posted 3 years ago by araid-en10
    What tool root #q
  • posted 2 years ago by kevitalin39
    Tool root #q
  • posted 2 years ago by adrieljaredtubong
  • posted 2 years ago by jackpot1136
    I have to say thank you for your help.
  • posted 2 years ago by comyoussef
  • posted 2 years ago by sherrielee
    Please can you help i dont understand this its not working
  • posted 2 years ago by doge344
  • posted 2 years ago by shenk0786
    Please help on this
  • posted a year ago by stsl
  • posted a year ago by tamu18115
  • posted a year ago by chatjan0922
  • posted a year ago by nelson1576
    Please help me out
  • posted a year ago by 5b899289de269400127763f4
    Ayuda por fabor
  • posted a year ago by cashcashn
    I can't figure out guys umm umm
  • posted a year ago by sohelshaikh5465
    Sohel shaikh 5465
  • posted a year ago by viloka
    I need some helping hands here
  • posted a year ago by anspage
  • posted a year ago by dgb
  • posted a year ago by murdy
  • posted a year ago by malayanhac
    good morning to all SIFU And MahaGuru, i dont know what the mission today, malaysia amex huntersWas RIP.
  • posted 9 months ago by because67
    How do I delete this account
  • posted 7 months ago by wow
  • posted 7 months ago by ultraprecisekamal
    ()=>`Thank you runkit team`
  • posted 6 months ago by 5d44a5261549b3001313407f
  • posted 5 months ago by 5d5d647b2ddea900148b41fe Pourquoi ça ne reduise pas en 1?
  • posted 2 months ago by 5db066a397504b001306620e

sign in to comment