Back to JavaScript
2026-01-165 min read

Function Apply (JavaScript)

Learn Function Apply (JavaScript) step by step with clear examples and exercises.

Title: Function apply() Method in JavaScript - A full guide

Why This Matters

The apply() method in JavaScript is a powerful tool that allows you to call a function with a specific set of arguments, even if those arguments are stored in an array or another object. Understanding how to use apply() can help you write more flexible and reusable code, especially when working with functions that take variable numbers of arguments. This skill is crucial for real-world programming scenarios, such as event handling, AJAX requests, and third-party library integrations.

The Importance of Flexible Function Calls

Flexibility in function calls can greatly improve the maintainability and readability of your code. By using apply(), you can create functions that can be easily reused with different sets of arguments or objects, reducing the need for multiple similar functions. This leads to cleaner and more efficient code.

Prerequisites

Before diving into the apply() method, it's essential to have a solid understanding of the following topics:

  1. JavaScript basics: variables, data types, operators, control structures (if/else, loops)
  2. Function declarations and expressions
  3. Calling functions with arguments
  4. Arrays and objects in JavaScript
  5. Understanding the concept of this and how it works in JavaScript
  6. Comprehension of arrow functions and their differences from regular functions

Core Concept

The apply() method is a built-in function that belongs to every JavaScript object. It allows you to call another function with a specific set of arguments, which can be provided as an array or as individual values. The syntax for the apply() method is:

function.apply(thisArg, [argsArray])
  • function: the function you want to call using apply().
  • thisArg (optional): the value of this within the called function. If not provided, this will refer to the global object (window in a browser environment).
  • argsArray (optional): an array containing the arguments to pass to the called function. If not provided, no arguments are passed.

Understanding the Role of thisArg and argsArray

The thisArg parameter allows you to control the value of this within the called function. If you don't provide a thisArg, it will default to the global object (window in a browser environment). The argsArray parameter is an array containing the arguments to pass to the called function.

Worked Example

Let's create a simple example to demonstrate how the apply() method works:

function greet(name) {
console.log(`Hello, ${name}!`);
}

const names = ['Alice', 'Bob', 'Charlie'];

names.forEach((name) => {
greet.apply(null, [name]); // Call the greet function with each name from the array
});

In this example, we define a greet() function that logs a personalized greeting message. We then create an array of names and use the forEach() method to call the greet() function for each name in the array using the apply() method. The null argument is used as the value of this, which means that it won't affect the execution of the greet() function.

Understanding the Value of this in apply()

When using apply(), it's essential to understand that the value of this within the called function will be thisArg. If you don't provide a thisArg, it will default to the global object (window in a browser environment).

const obj = { name: 'John' };

function greet() {
console.log(`Hello, ${this.name}!`);
}

greet(); // Hello, undefined! (global `this`)
greet.apply(obj); // Hello, John! (`this` is set to obj)

Common Mistakes

  1. Forgetting to pass arguments: If you don't provide an array or individual arguments when using apply(), the called function will not receive any arguments, and it may throw an error if it expects them.
function sum(a, b) {
return a + b;
}

sum.apply(); // Uncaught TypeError: sum() requires 2 arguments
  1. Misunderstanding the value of this: When using apply(), it's essential to understand that the value of this within the called function will be thisArg. If you don't provide a thisArg, it will default to the global object (window in a browser environment).
  1. Assuming call() and apply() are interchangeable: Although both call() and apply() allow you to call a function with specific arguments, they have subtle differences in how they handle the value of this. In general, use call() when you need to pass individual arguments and want to control the value of this, while using apply() when you're working with an array of arguments.

Common Mistake - Using apply() with Arrow Functions

Arrow functions do not have their own this, so you cannot use apply() with them directly. However, you can work around this by using a regular function and binding the this value before calling it with apply().

const obj = { name: 'John' };

const greetArrow = () => console.log(`Hello, ${this.name}!`);
const greetRegular = greetArrow.bind(obj); // Bind the value of this to obj
greetRegular(); // Hello, John!

Practice Questions

  1. Write a function multiply(a, b) that multiplies two numbers. Use apply() to call this function with the arguments stored in an array.
// Your solution here
function multiply(a, b) {
return a * b;
}

const args = [3, 4];
console.log(multiply.apply(null, args)); // Output: 12
  1. Create an object myObj with properties name, age, and city. Write a function displayDetails() that logs these properties. Use apply() to call this function using the myObj object as thisArg.
// Your solution here
const myObj = { name: 'Alice', age: 25, city: 'New York' };

function displayDetails() {
console.log(`Name: ${this.name}, Age: ${this.age}, City: ${this.city}`);
}

displayDetails.apply(myObj); // Output: Name: Alice, Age: 25, City: New York

FAQ

Can I use apply() with arrow functions?

No, arrow functions do not have their own this, so you cannot use apply() with them directly. However, you can work around this by using a regular function and binding the this value before calling it with apply().

What happens if I don't provide thisArg when using apply()?

If you don't provide thisArg, the value of this within the called function will be the global object (window in a browser environment). This might not always be what you intended, so it's essential to understand how this behaves when using apply().

Is there a difference between call() and apply()?

Although both call() and apply() allow you to call a function with specific arguments, they have subtle differences in how they handle the value of this. In general, use call() when you need to pass individual arguments and want to control the value of this, while using apply() when you're working with an array of arguments.

Can I use apply() with regular functions and arrow functions interchangeably?

No, due to the differences in how this behaves between regular functions and arrow functions, you cannot use apply() interchangeably with both types of functions. Regular functions rely on thisArg for setting the value of this, while arrow functions do not have their own this.

Function Apply (JavaScript) | JavaScript | XQA Learn