0% found this document useful (0 votes)
5 views8 pages

JavaScript Function Apply() Method

The document explains the JavaScript apply() method, which allows a function to be called with a specified this value and arguments provided as an array. It contrasts apply() with call(), noting that apply() accepts arguments as an array while call() takes them individually. Additionally, it demonstrates how to use apply() with examples, including finding the maximum value in an array using Math.max().

Uploaded by

samerguda13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

JavaScript Function Apply() Method

The document explains the JavaScript apply() method, which allows a function to be called with a specified this value and arguments provided as an array. It contrasts apply() with call(), noting that apply() accepts arguments as an array while call() takes them individually. Additionally, it demonstrates how to use apply() with examples, including finding the maximum value in an array using Math.max().

Uploaded by

samerguda13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

 Tutorials  Exercises  Services   Get Certified Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++

JavaScript Function apply()


‹ Previous Next ›

Method Reuse
With the apply() method, you can write a method that can be used on different
objects.

The JavaScript apply() Method


The apply() method is similar to the call() method (previous chapter).

In this example the fullName method of person is applied on person1:

Example

const person = {
fullName: function() {
return this.firstName + " " + this.lastName;
}
}

const person1 = {
firstName: "Mary",
lastName: "Doe"
}

https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 1 of 8
:
// This will return "Mary Doe":
person.fullName.apply(person1);

Try it Yourself »

The Difference Between call() and apply()


The difference is:

The call() method takes arguments separately.

The apply() method takes arguments as an array.

The apply() method is very handy if you want to use an array instead of an argument
list.

The apply() Method with Arguments


The apply() method accepts arguments in an array:

Example

const person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," +
country;
}
}

const person1 = {
firstName:"John",
lastName: "Doe"
}

person.fullName.apply(person1, ["Oslo", "Norway"]);

https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 2 of 8
:
Try it Yourself »

Compared with the call() method:

Example

const person = {
fullName: function(city, country) {
return this.firstName + " " + this.lastName + "," + city + "," +
country;
}
}

const person1 = {
firstName:"John",
lastName: "Doe"
}

person.fullName.call(person1, "Oslo", "Norway");

Try it Yourself »

ADVERTISEMENT

https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 3 of 8
:
Simulate a Max Method on Arrays
You can find the largest number (in a list of numbers) using the Math.max() method:

Example

Math.max(1,2,3); // Will return 3

Try it Yourself »

Since JavaScript arrays do not have a max() method, you can apply the Math.max()
method instead.

Example

Math.max.apply(null, [1,2,3]); // Will also return 3

Try it Yourself »

The first argument (null) does not matter. It is not used in this example.

These examples will give the same result:

Example

Math.max.apply(Math, [1,2,3]); // Will also return 3

Try it Yourself »

Example

Math.max.apply(" ", [1,2,3]); // Will also return 3

https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 4 of 8
:
Try it Yourself »

Example

Math.max.apply(0, [1,2,3]); // Will also return 3

Try it Yourself »

JavaScript Strict Mode


In JavaScript strict mode, if the first argument of the apply() method is not an
object, it becomes the owner (object) of the invoked function. In "non-strict" mode, it
becomes the global object.

‹ Previous Next ›

W3schools Pathfinder
Track your progress - it's free! Sign Up Log in

ADVERTISEMENT
___

https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 5 of 8
:
COLOR PICKER



ADVERTISEMENT

https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 6 of 8
:
 PLUS SPACES GET CERTIFIED

FOR TEACHERS FOR BUSINESS CONTACT US

Top Tutorials Top References


HTML Tutorial HTML Reference
CSS Tutorial CSS Reference
JavaScript Tutorial JavaScript Reference
How To Tutorial SQL Reference
SQL Tutorial Python Reference
Python Tutorial W3.CSS Reference
W3.CSS Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms
of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 7 of 8
:
https://www.w3schools.com/js/js_function_apply.asp 02/11/2024, 10 58 AM
Page 8 of 8
:

You might also like