=> why?

I’m not understanding the reason to use “=>” between the parentheses and the brackets when making a function. example: let whyDoThis = () => {}; instead of letWhyNotJustDoThis = () {};

its just part of the syntax, we need => as part of the syntax to indicate a function

1 Like

How is that syntax better than:

function myFunction(){

What’s the point of declaring the function as a const, then saying it’s a function, then drawing an arrow, then writing the function? Seems unnecessary.

well, for starters declaring a function as constant means it can’t be re-assigned, so its more memory efficient.

the new es6 is a lot better, it properly bind this keyword:

function Person(){
  this.age = 0;

  setInterval(() => {
    this.age++; // |this| properly refers to the person object
  }, 1000);

var p = new Person();

which makes it much better for OOP.

if we attempt this in pre-es6 syntax we get an error:

function Person() {
  this.age = 0;

  setInterval(function growUp() {
  }, 1000);

var p = new Person();

we get an error, and would have to do annoying tricks to make it work.

The arrow syntax is great, and very useful as you will see when you continue your journey. It solved some of the problems the JavaScript language has.

yes, it might look strange at first, but its very useful. The different ways to declare function in javascript each have its pros and cons.

Learn the different ways you can declare a function, then learn the use cases. You will see


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.