Skip to the page

Functions and this · core

Function forms and arrow functions

Functions are values you can store, pass and return; arrow functions are a shorter form that has no own this, arguments, super or prototype and cannot be called with new.

The notes

What you must know

  • Forms: declaration (hoisted), expression (named or anonymous), arrow, method shorthand, class method, generator, async function.
  • Higher-order functions take or return functions: map, debounce, middleware, React hooks factories.
  • arguments is array-like (not an array) and doesn't exist in arrow functions. Prefer rest parameters (...args).
  • Default parameters are evaluated at call time, only when the argument is undefined (not null).
  • fn.length counts parameters before the first default or rest parameter. curry() depends on it.
  • Arrow functions: this comes from the surrounding scope when the arrow is created; no own arguments; cannot be constructors (new throws TypeError); no prototype property; call, apply and bind can't change their this.
  • Avoid arrows for object methods that use this, prototype methods, constructors and generators. Arrow class fields (handle = () => {}) bind this per instance but create one function per instance and can't be spied on via the prototype.

The red pen

Common traps: mistake → fix

  • Mistake: Arrow functions are just shorter syntax

    Fix: they differ in this, arguments, new and prototype.

  • Mistake: fn.length is the number of arguments passed

    Fix: it is the number of declared parameters before defaults and rest.

  • Mistake: An arrow's this is the object literal it sits in

    Fix: object literals don't create a scope; the arrow takes this from the enclosing function.

Easy to hard

Questions interviewers ask, easy to hard

  1. Function declaration vs expression vs arrow: what is different?
  2. When would you NOT use an arrow function?
  3. What is arguments? Why prefer rest parameters?
  4. Arrow class field vs prototype method: what are the trade-offs?
  5. Why can't you call new on an arrow function? What is new.target?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview