33

I have seen some functions defined as function(){} and some functions defined as () => {}.

Is this related to Javascript version ES6?

Also, how does use of this keyword change from one function definition to another?

cyberbit
  • 1,345
  • 15
  • 22
Parth Tiwari
  • 855
  • 2
  • 9
  • 23
  • `() => {}` is called an arrow function. Here are the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – gen_Eric Jun 13 '16 at 18:25
  • A good in-depth article on normal functions vs. arrow functions: https://hacks.mozilla.org/2015/06/es6-in-depth-arrow-functions/ – Wavesailor Jun 13 '19 at 17:09

1 Answers1

39

The () => {} is called an arrow function. They are, as you said, part of ES6. From the linked page:

An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous.

cyberbit
  • 1,345
  • 15
  • 22