Tutorials Exercises Services Get Certified Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] 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 [Link] + " " + [Link];
}
}
const person1 = {
firstName: "Mary",
lastName: "Doe"
}
[Link] 02/11/2024, 10 58 AM
Page 1 of 8
:
// This will return "Mary Doe":
[Link](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 [Link] + " " + [Link] + "," + city + "," +
country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
[Link](person1, ["Oslo", "Norway"]);
[Link] 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 [Link] + " " + [Link] + "," + city + "," +
country;
}
}
const person1 = {
firstName:"John",
lastName: "Doe"
}
[Link](person1, "Oslo", "Norway");
Try it Yourself »
ADVERTISEMENT
[Link] 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 [Link]() method:
Example
[Link](1,2,3); // Will return 3
Try it Yourself »
Since JavaScript arrays do not have a max() method, you can apply the [Link]()
method instead.
Example
[Link](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
[Link](Math, [1,2,3]); // Will also return 3
Try it Yourself »
Example
[Link](" ", [1,2,3]); // Will also return 3
[Link] 02/11/2024, 10 58 AM
Page 4 of 8
:
Try it Yourself »
Example
[Link](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
___
[Link] 02/11/2024, 10 58 AM
Page 5 of 8
:
COLOR PICKER
ADVERTISEMENT
[Link] 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 [Link] Reference
[Link] 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
[Link] 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
[Link].
[Link] 02/11/2024, 10 58 AM
Page 7 of 8
:
[Link] 02/11/2024, 10 58 AM
Page 8 of 8
: