• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • apply()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)
      • 正體中文 (繁體)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. apply()
    2. [Symbol.hasInstance]()
  • Instance properties
    1. displayName Non-standard
    2. arguments Non-standard Deprecated
    3. caller Non-standard Deprecated
  • Inheritance
  • call()
  • displayName Non-standard
  • arguments Non-standard Deprecated
  • caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. array-like object).

  • Try it

    const numbers = [5, 6, 2, 3, 7];
    
    const max = Math.max.apply(null, numbers);
    
    console.log(max);
    / Expected output: 7
    
    const min = Math.min.apply(null, numbers);
    
    console.log(min);
    / Expected output: 2
    

    Syntax

    js
    apply(thisArg)
    apply(thisArg, argsArray)
    

    Parameters

    thisArg

    The value of this provided for the call to func. If the function is not in undefined will be replaced with the global object, and primitive values will be converted to objects.

    argsArray Optional

    An array-like object, specifying the arguments with which func should be called, or undefined if no arguments should be provided to the function.

    Return value

    The result of calling the function with the specified this value and arguments.

    Description

    Note: This function is almost identical to call(), except that the function arguments are passed to call() individually as a list, while for apply() they are combined in one object, typically an array — for example, func.call(this, "eat", "bananas") vs. func.apply(this, ["eat", "bananas"]).

    Normally, when calling a function, the value of this inside the function is the object that the function was accessed on. With apply(), you can assign an arbitrary value as this when calling an existing function, without first attaching the function to the object as a property. This allows you to use methods of one object as generic utility functions.

    You can also use any kind of object which is array-like as the second parameter. In practice, this means that it needs to have a length property, and integer ("index") properties in the range (0..length - 1). For example, you could use a arguments, for example:

    js
    function wrapper() {
      return anotherFn.apply(null, arguments);
    }
    

    With the spread syntax, this can be rewritten as:

    js
    function wrapper(...args) {
      return anotherFn(...args);
    }
    

    In general, fn.apply(null, args) is equivalent to fn(...args) with the parameter spread syntax, except args is expected to be an array-like object in the former case with apply(), and an iterable object in the latter case with spread syntax.

    Warning: Do not use apply() to chain constructors (for example, to implement inheritance). This invokes the constructor function as a plain function, which means extends instead.

    Examples

    Using apply() to append an array to another

    You can use Array.prototype.concat() does have the desired behavior in this case, but it does not append to the existing array — it creates and returns a new array.

    In this case, you can use apply to implicitly "spread" an array as a series of arguments.

    js
    const array = ["a", "b"];
    const elements = [0, 1, 2];
    array.push.apply(array, elements);
    console.info(array); / ["a", "b", 0, 1, 2]
    

    The same effect can be achieved with the spread syntax.

    js
    const array = ["a", "b"];
    const elements = [0, 1, 2];
    array.push(...elements);
    console.info(array); / ["a", "b", 0, 1, 2]
    

    Using apply() and built-in functions

    Clever usage of apply() allows you to use built-in functions for some tasks that would probably otherwise require manually looping over a collection (or using the spread syntax).

    For example, we can use Math.min() to find out the maximum and minimum value in an array.

    js
    / min/max number in an array
    const numbers = [5, 6, 2, 3, 7];
    
    / using Math.min/Math.max apply
    let max = Math.max.apply(null, numbers);
    / This about equal to Math.max(numbers[0], …)
    / or Math.max(5, 6, …)
    
    let min = Math.min.apply(null, numbers);
    
    / vs. loop based algorithm
    max = -Infinity;
    min = Infinity;
    
    for (const n of numbers) {
      if (n > max) {
        max = n;
      }
      if (n < min) {
        min = n;
      }
    }
    

    But beware: by using apply() (or the spread syntax) with an arbitrarily long arguments list, you run the risk of exceeding the JavaScript engine's argument length limit.

    The consequences of calling a function with too many arguments (that is, more than tens of thousands of arguments) is unspecified and varies across engines. (The JavaScriptCore engine has a hard-coded argument limit of 65536.) Most engines throw an exception; but there's no normative specification preventing other behaviors, such as arbitrarily limiting the number of arguments actually passed to the applied function. To illustrate this latter case: if such an engine had a limit of four arguments (actual limits are of course significantly higher), it would be as if the arguments 5, 6, 2, 3 had been passed to apply in the examples above, rather than the full array.

    If your value array might grow into the tens of thousands, use a hybrid strategy: apply your function to chunks of the array at a time:

    js
    function minOfArray(arr) {
      let min = Infinity;
      const QUANTUM = 32768;
    
      for (let i = 0; i < arr.length; i += QUANTUM) {
        const subMin = Math.min.apply(
          null,
          arr.slice(i, Math.min(i + QUANTUM, arr.length)),
        );
        min = Math.min(subMin, min);
      }
    
      return min;
    }
    
    const min = minOfArray([5, 6, 2, 3, 7]);
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-function.prototype.apply

    Browser compatibility

    See also

    • arguments
    • Function.prototype.bind()
    • Function.prototype.call()
    • Reflect.apply()
    • Functions
    • Spread syntax (...)

    Help improve MDN

    function keyword (as in function () { }) and arrow (=>) expression create functions. The JavaScript functions protocol includes default and rest parameters and binding to this.","name":"Functions"}},"browserCompat":["javascript.builtins.Function.apply"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant