JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptFunctions
PrevNext
javascript
beginner
9 min read

Functions

arrow-functions
es6
functions
fundamentals
this

Arrow functions lexically bind 'this' from their enclosing scope and have concise syntax, while regular functions dynamically bind 'this' based on how they're called and can be used as constructors.

Key Points

1Arrow Function this

Lexically bound from enclosing scope at definition time — cannot be changed with call/apply/bind

2Regular Function this

Dynamically bound based on how it's called — object method, new, call/apply/bind, or default global/undefined

3Arrow Limitations

No arguments object, no prototype, cannot be constructors (new throws TypeError), cannot be generators

4When to Use Each

Arrows for callbacks and preserving this; regular for object methods, constructors, and when you need arguments

5Default and Rest Params

Default values activate on undefined (not null); rest (...args) collects remaining args into a real array

What You'll Learn

  • Understand the difference between arrow functions and regular functions
  • Know how this binding differs between arrow and regular functions
  • Know when to use arrow functions vs regular functions

Deep Dive

JavaScript has two main function syntaxes: regular functions (function declarations/expressions) and arrow functions (ES6). Understanding their differences — especially around this binding — is one of the most frequently asked interview topics.

Regular Functions

  • Function declarations: function foo() {} — fully hoisted, can be called before they appear in code.
  • Function expressions: const foo = function() {} — follow variable hoisting rules (not available before assignment).
  • this is dynamically bound based on how the function is called: as an object method (this = the object), with new (this = new instance), with call/apply/bind (this = explicit value), or standalone (this = global object or undefined in strict mode).
  • Have their own arguments object — an array-like object containing all passed arguments.
  • Can be used as constructors with new — creates a new object and sets this to it.
  • Have a prototype property used for prototype-based inheritance.

Arrow Functions

  • Concise syntax: (params) => expression for single expressions (implicit return), (params) => { statements } for blocks.
  • this is lexically bound — it captures this from the enclosing scope at definition time and cannot be changed. call(), apply(), and bind() cannot override it.
  • No arguments object — use rest parameters (...args) instead.
  • Cannot be used as constructors — new (() => {}) throws TypeError.
  • No prototype property.
  • Cannot be used as generators (no yield).

When to Use Each

  • Arrow functions: callbacks, event handlers, array methods (.map(), .filter()), any context where you want to preserve the surrounding this.
  • Regular functions: object methods that need this to refer to the object, constructors, functions that need arguments object, generator functions.
  • In React: arrow functions in event handlers prevent accidental this binding issues. Class methods often use arrow function class fields to auto-bind this.

Default Parameters and Rest Parameters

  • Default values: function greet(name = 'World') {} — the default is used when the argument is undefined (not when it's null or false).
  • Rest parameters: function sum(...numbers) {} — collects remaining arguments into a real array (unlike arguments which is array-like). Must be the last parameter.

Key Interview Distinction: this in Arrow vs Regular Regular functions: this depends on how the function is called (dynamic). Arrow functions: this is inherited from where the function is defined (lexical). This is the single most important difference. A common follow-up: "Why can't you use arrow functions as object methods?" — because this would refer to the enclosing scope, not the object.

Fun Fact

Arrow functions were the most controversial ES6 feature during the TC39 proposal process. The committee debated the syntax for over 2 years — alternatives included fn(x) { x + 1 }, lambda(x) { x + 1 }, and (x) -> x + 1. The fat arrow => won partly because CoffeeScript had already popularized it, proving developers found it readable.

Continue Learning

The this Keyword

intermediate

Practice What You Learned

What is the difference between arrow functions and regular functions?
junior
functions
Arrow functions have a shorter syntax, don't have their own 'this' binding (they inherit from the enclosing scope), cannot be used as constructors, and don't have access to the 'arguments' object.
Previous
Events
Next
Generators & Iterators
PrevNext