-2

I am increasingly seeing javascript code where functions are being defined as

const funcName = () => {

};

Is this a better way of declaring functions now that we have ES6 :

runtimeZero
  • 22,318
  • 19
  • 63
  • 115
  • What you wrote down **IS** ES6 (see [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Syntax) link) – kutacoder Nov 17 '15 at 14:46
  • Yeah, he's asking if it's a better way of declaring than the old ways of writing function expressions. – Keith Rousseau Nov 17 '15 at 14:47
  • i know..and I am looking for reasons to use this syntax over regular syntax. – runtimeZero Nov 17 '15 at 14:47
  • In most cases it's simply wankers holding on too tight. For the right type of function there is a benefit – Jaromanda X Nov 17 '15 at 14:48
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Mark C. Nov 17 '15 at 14:48
  • 1
    Are you referring to `const` or the arrow function, or both? How to define a function primarily depends on how it is going to be used. There is no "better", there is only "pros" and "cons". – Felix Kling Nov 17 '15 at 14:48
  • 1
    Use a shovel to dig a hole. Use a screwdriver to scrape the gunk out from under your toenails. Always use the right tool for the job – Jaromanda X Nov 17 '15 at 14:52
  • What do you mean with "better"? Arrow function expressions are not equivalent to function expressions. Arrow functions, for example, lexically binds the `this` value and cannot be `new`ed. – Paolo Moretti Nov 17 '15 at 14:52

1 Answers1

1

Is this a better way of declaring functions now that we have ES6.

No. It's just a (very concise) way to do it. Every kind of function definition has slightly different behavior and therefore different pros and cons. Use the one that fulfills your need and communicates your intend.

Some resources:

Community
  • 1
  • 1
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072