JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Function Apply


📘 JavaScript apply() Method – Call a Function with Array Arguments

The apply() method lets you invoke a function with a specified this context and an array (or array-like object) of arguments.


🔹 Syntax

func.apply(thisArg, [argsArray]);
  • thisArg: The value to use as this inside the function.

  • argsArray: An array (or array-like object) of arguments.


🔹 Example 1 – Basic Usage

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

const person = { name: "Alice" };

greet.apply(person, ["Delhi", "India"]);
// Output: Hello Alice from Delhi, India

🔹 Example 2 – Using Math.max() with apply

const numbers = [4, 9, 2, 7];

const max = Math.max.apply(null, numbers);
console.log(max); // Output: 9

🔹 Difference: call() vs apply()

Method Argument Format
call() Comma-separated values
apply() Array of arguments

Practice Questions

Q1. Create a function showInfo that logs this.name, this.age. Use apply() to invoke it with { name: "Ravi", age: 28 }.

Q2. Write a function sum(a, b) and call it using apply() with [4, 6] as arguments and a dummy this object.

Q3. Create an object student with name: "Anu" and use a standalone printName() function with apply() to print her name.

Q4. Use apply() with Math.min to find the smallest number in [2, 8, 5, 1, 9].

Q5. Create a function introduce(city, country) and use apply() to bind it with a person object and pass array arguments.

Q6. Create a function calculateArea(length, width) and use apply() to pass [5, 3] as arguments.

Q7. Define a sayHello function and use apply() to call it with different this values like { name: "Sara" }, { name: "Tom" }.

Q8. Write a function showDetails() that logs this.id and this.role. Use apply() with { id: 101, role: "Admin" }.

Q9. Create a function multiply(a, b, c) and call it using apply() with array [2, 3, 4]. Print the result.

Q10. Write a function concatStrings(a, b, c) and use apply() to pass ["JavaScript", "is", "fun"] and print "JavaScript is fun".


Function Apply Quiz

Q1: What is the primary purpose of apply()?

A. Bind a function permanently
B. Call a function with a custom this and array of arguments
C. Clone a function
D. Return a function

Q2: Which of the following uses apply() correctly with Math.max?

A. Math.max.apply([1, 2, 3])
B. Math.max.apply(null, [1, 2, 3])
C. Math.max.apply(this, 1, 2, 3)
D. Math.max.apply([1, 2, 3], null)

Q3: What is the second argument of apply() expected to be?

A. Object
B. String
C. Array or array-like object
D. Boolean

Q4: Which is true about apply() vs call()?

A. apply() takes individual args, call() takes an array
B. Both are the same
C. call() uses individual args, apply() uses array
D. apply() only works with arrow functions

Q5: Which is a correct signature of the apply() method?

A. function.apply(args)
B. function.apply(thisArg, [args])
C. function.apply([thisArg, args])
D. function.apply(null)

Q6: What happens if the second argument to apply() is not an array?

A. It's ignored
B. It causes an error
C. It’s converted automatically
D. It’s treated as one argument

Q7: Which of the following best demonstrates reusing a function for multiple objects using apply()?

A. greet.call(object)
B. greet.bind(object)
C. greet.apply(object, ["arg1", "arg2"])
D. object.greet()

Go Back Top