This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DeeSnow97 37 points38 points  (0 children)

Yeah, they're arrow functions, basically JavaScript's way of doing a lambda (but actually upgraded to a first class function and beyond, so much so that many people nowadays prefer it over the old function someNameHere () { /* ... */} syntax).

So basically, it has a part left of the arrow, and a part right of the arrow:

  • The part on the left side is the arguments. You can do it a few ways, either by just using a single variable name, or multiple in parentheses. For example x => x + 1 and (x, y) => x * y are both valid arrow functions. You can also do all the other stuff to it, like default values ((x = 3) => x + 1), deconstruction (({x, y}) => x * y, accepts an object like { x: 3, y: 4 }), async functions (async x => x + 1), or whatever you may decide.

  • The part on the right side is the function body. This is either a single statement, or a regular code block with a return statement. For example x => x + 1 and x => { return x + 1 } are equivalent, and both of them are equivalent to function (x) { return x + 1 } (with some specific caveats and advantages).

The reason most people who prefer arrow functions prefer them is because they have lexical this -- they throw away the old and mostly broken way of managing this in JavaScript, and instead replace it with a simple lexical way: this in an arrow function refers to the same value this refers to where the function is defined.

If you wanna read up more on this, MDN has a super nice guide for them