Function expressions vs arrow functions

I would create a poll, but can’t seem to find that functionality. I’m assuming Discourse doesn’t have that.

How many of you prefer function expressions, and how many prefer arrow functions?

I’m just now reading about arrow functions, and I’m not sure if I want to use them. Just trying to get a feel for how the community leans in this.

V/r,

^ _ ^

  • Function expressions
  • Arrow functions

0 voters

Offtopic: I’ve added a poll for you. You can create one like so:

[poll]
- Option 1
- Option 2
[/poll]
2 Likes

I prefer arrow functions because they don’t take up space in the symbol table, and it’s more clear that it’s a function, whereas adding () to a variable name to execute it just seems odd.

Also, putting a function in a variable smells like state (as it could be overriden - unless it is a const of course) and I don’t like state.

Cool! Thanks for the heads-up.

V/r,

^ _ ^

I’m surprised this isn’t mentioned in the tutorial: https://learnxinyminutes.com/docs/markdown/

V/r,

^ _ ^

I voted for arrow function, as the implicit return makes for some nice to read code:

const even = numbers.filter(num => num % 2 === 0);

Also the this value of arrow functions is bound to their parent scope, meaning that they are not always a drop in replacement for regular functions. Again, this can lead to some nice to read code (no more var that = this).

2 Likes

I think you’re confusing arrow functions with anonymous function expressions… regular function expressions can be also anonymous, and both can be assigned to a variable (whether they are anonymous or not):

// arrow function expression
const foo = () => null

// anonymous function expression
const bar = function () { return null }

// named function expression
const baz = function baz () { return null }

As for the parens after variables – well how would you invoke a function otherwise (besides IIFEs), especially an arrow function that can’t have a name by itself?

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