Function apply()

Why function.apply ?

The JavaScript apply () function is used to recall a function. Apply () contains the value and parameters in the assigned function.

Syntax:

function.apply(thisArg, [array])  

thisArg(optional): The this value is given for the call to a function.

array(optional): It is an array-like object.


Let’s understand the logic with a small example.

        function sum(a,b) {
            return a + b;
        }
        
        let res = sum.apply(null,[2,5])
        console.log(res)

7


The apply () method is very similar to the call () method. The apply () method takes an array as a variable. The Call () method takes individual arguments.

        let users = {
            user1: function (name, surname) {
                return "age: " + this.age + "  " + "country: " + this.country;
            }
        }
        let use1 = {
            age: 32,
            country: "England"
        }

        let arr = users.user1.apply(use1, ["rick", "allen"])
        console.log(arr)

age: 32 country: England

call;

        let users = {
            user1: function ( name, surname ) {
                return "age: " + this.age + "  " + "country: " + this.country;
            }
        }
        let use1 = {
            age: 32,
            country: "England"
        }

        let arr = users.user1.call( use1, "rick", "allen")
        console.log(arr)

age: 32 country: England


Function apply() Examples

Example 1

        function sum() {
            console.log(this);
            let sum = 0;
            for (var i = 0; i < arguments.length; i++) {
                sum += arguments[i];
            }
            return sum;
        }

        let result = sum.apply({
            name: "barry",
            surname: "quenn"
        }, [ 10 , 20 , 30 , 40]);
        console.log(result);

output:

name: “barry”, surname: “quenn”
100


Example 2

        let users = {
            nameAndSurname: function () {
                return this.name + " -" + this.surname;
            }
        }

        let user1 = { name: "oliver", surname: "clark" };
        let user2 = { name: "bob", surname: "adams" };
        let user3 = { name: "alex", surname: "black" };

        console.log( users.nameAndSurname.apply( user1 ) );
        console.log( users.nameAndSurname.apply( user2 ) );
        console.log( users.nameAndSurname.apply( user3 ) );

output:

oliver — clark
bob — adams
alex — black


Example 3

        let numbers = [ 1 , 2 , 3 , 4 , 5 , 6 ];
        let max = Math.max.apply(null, numbers);
        console.log(max);

output:

6


Example 4

        let numbers = [1, 2, 3, 4, 5, 6];
        let min = Math.min.apply(null, numbers);
        console.log(min);

output:

1


Example 5

        let txt = ["a", "b", "c", "d", "e"];
        let numbers = [1, 2, 3, 4, 5, 6];
        txt.push.apply(txt, numbers);
        console.log(txt);

output:

[“a”, “b”, “c”, “d”, “e”, 1, 2, 3, 4, 5, 6]


Example 6

        function nameAndUsername(name, job) {
            return "my name is " + name + "and I am a " + job
        }

        console.log(nameAndUsername.apply(null, ["rick", "teacher"]));
        console.log(nameAndUsername.apply(null, ["bob", "mathematician"]));
        console.log(nameAndUsername.apply(null, ["alex", "physic"]));

output:

my name is rickand I am a teacher
my name is boband I am a mathematician
my name is alexand I am a physic


Example 7

        let friend = {
            car: true,
            lendCar: function (a) {
                this.car = a;
            }
        }

        let me = {
            car: true,
            gotCar: function () {
                return this.car === true;
            }
        };

        friend.lendCar.apply(me, [true]);
        console.log(me.gotCar());

output:

true


Example 8

apply
        let num1 = Number(prompt("number1: "));
        let num2 = Number(prompt("number2: "));

        function sum(a, b) {
            return this.a + this.b;
        };

        let num = {
            a: num1,
            b: num2,
        }

        let res = sum.apply(num);

        console.log(res)

Browser Support

Chromeyes
Edgeyes
Firefox1
Internet Exploreryes
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

4 thoughts on “Function apply()

Add yours

  1. I really love your site.. Excellent colors & theme. Did you develop this amazing site yourself?

    Please reply back as I’m trying to create my
    very own blog and would like to learn where you got this
    from or exactly what the theme is named. Appreciate
    it!

    Liked by 1 person

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started