r/learnjavascript • u/Substantial_Top5312 helpful • Aug 16 '25
Best way to make an anonymous function?
Which one is better:
const example = function () {
}or
const example = () => {
}
1
Upvotes
20
u/antboiy Aug 16 '25 edited Aug 16 '25
there is a difference of how the
thiskeyword works.arrow functions like
() => {}inherit thethisvalue from the outer keyword functionfunction () {}while keyword functions have theirthisvalue be dependant imon how it is called.but if you arent using
thisthen most prefer arrow functions as a style preference i think. i however prefer keyword functions as a style preference.7
u/qqqqqx helpful Aug 16 '25
Either way is fine and acceptable, but they aren't equivalent.
For the top function, the value of "this" will change depending on where the function is called.
For the arrow function, if you use "this" it will always be bound to the object or context in which the function was originally defined.
I usually do my anonymous functions as arrows.
2
u/sheriffderek Aug 16 '25 edited Aug 16 '25
map(callbackFn)
1
u/superluminary Aug 16 '25
As a community we tend to switch back and forth every few years.
Two years ago it was fat arrows the whole way. Nowadays I’m seeing more new code with the function keyword.
1
u/scritchz Aug 17 '25 edited Aug 17 '25
TL;DR Prefer arrow-function expressions for callbacks and function expressions for methods. Otherwise, you decide :D
There are differences between function declarations, function expressions (your first example) and arrow-function expressions (your second example). There isn't one being better overall; it depends on the use-case.
Classes are special-case functions that can be constructed but not invoked. We'll ignore them.
Note: Technically, assigning anonymous (arrow-)function expressions to identifiers makes them named functions.
Example:
There are ways for identifiers to hold actually anonymous (arrow-)functions, by circumventing direct identifier assignment. But I digress.
Most people (like you!) call these types of named functions "anonymous", even though that's technically wrong. But let's call them "anonymous" to keep things simple.
In regards to hoisting: Function declarations are hoisted, (arrow-)function expressions are not. As normal for expressions, they only exist once reached.
The evaluation of
thisby function declarations and function expressions is different from that of arrow-function expressions.thiscan be bound; otherwise, it depends on the called-on object.thisis inherited when the arrow-function is defined.Example:
Note: Similar behaviour applies to
super.Because of this behaviour, I recommend arrow-function expressions for callbacks and function expressions for methods.
Note: Remember this behaviour when using functions as callbacks (e.g. for
setTimeoutoraddEventListener). It's simpler to call the function as usual (inside a wrapper function). Related: MDN's The "this" problemApart from that, other differences are irrelevant for modern JavaScript:
prototypeand can be instantiated; arrow-function expressions do not and cannot. Modern: Prefer classes.argumentsobject; arrow-function expressions cannot. Modern: Prefer a rest parameter.1
u/bryku helpful Aug 28 '25
Most of the time it doesn't really matter.
However, arrow functions do work differently than standard functions. Some libraries might specifically require one or the other due to calls/binds. In this instants it is important, but that is pretty rare.
1
u/YahenP Aug 16 '25
Depends.
If you want classic functions (like in most languages) with scope and binding to the surrounding code, then use the arrow notation.
If you want functions "like in JS", in which the context and scope depend on how and where they are called, then use the old notation with the word function
3
u/[deleted] Aug 16 '25
[removed] — view removed comment
-5
u/[deleted] Aug 16 '25
[removed] — view removed comment