JavaScript · The language round
What is the difference between arrow and regular functions?
Short answer
Arrow functions capture this lexically, have no own arguments, cannot be constructors, and have no prototype. Regular functions get their own this and arguments and can be constructed.
The syntax difference is obvious. The behavioural differences matter more, especially around this, object methods and callbacks.
- Arrow functions do not bind their own
this; they use the enclosing scope'sthis - Arrow functions do not have their own
argumentsobject - Arrow functions cannot be used with
newand have noprototypeproperty - Arrow functions cannot be generators
- Regular functions can be constructors and can have their
thisset by the call site,call,applyorbind
const team = {
name: "Platform",
regular() {
return this.name;
},
arrow: () => this?.name,
};
console.log(team.regular()); // "Platform"
console.log(team.arrow()); // undefined in modulesthis, not the team object.Where arrows help
Arrows are a good fit for short callbacks that need the surrounding this, such as array methods inside a method or class. They are a poor fit for object methods, class methods that need the instance, and functions that need to be constructors.
const counter = {
count: 0,
start() {
setInterval(() => {
this.count += 1;
}, 1000);
},
};this of start().this from the caller, arguments, a prototype or a constructor, use a regular function.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1When should you avoid an arrow function?
- 2Why do arrow functions not have `arguments`?
- 3Can an arrow function be a constructor?
- 4How does an arrow function behave as an event handler?
Say it in the interview
Arrow functions capture this from the scope where they are written, while regular functions get this from how they are called. Arrows also have no own arguments, no prototype, and cannot be constructed. That makes arrows good for callbacks inside methods, but bad for object methods, class methods, and anything that needs to be used with new. If I need dynamic this or arguments, I use a regular function.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.