The Wayback Machine - https://web.archive.org/web/20221023022118/https://www.geeksforgeeks.org/javascript-function-call/
Skip to content
Related Articles

Related Articles

JavaScript Function Call

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Sep, 2022
View Discussion
Improve Article
Save Article

The Function call is a predefined javascript method, which is used to write methods for different objects. It calls the method, taking the owner object as an argument. The keyword this refers to the “owner” of the function or the object it belongs to. All the functions in javascript are considered object methods. So we can bound a function to a particular object by using ‘call()’. A function will be the global object if the function is not considered a method of a JavaScript object.

Syntax:

call()

Return Value: It calls and returns a method with the owner object being the argument.

Example 1: Here is a basic example to describe the use of the call() method. 

Javascript




<script>
    // function that returns product of two numbers 
    function product(a, b) {
        return a * b;
    }
  
    // Calling product() function
    var result = product.call(this, 20, 5);
  
    document.write(result);
</script>

Output:

Example 2: This example describes the use of function calls with arguments.

Javascript




<p id="GFG"></p>
  
<script>
    var employee = {
        details: function(designation, experience) {
            return this.name 
            + " " 
            + this.id + "<br>" 
            + designation 
            + "<br>" 
            + experience;
        }
    }
      
    // Objects declaration
    var emp1 = {
        name: "A",
        id: "123",
    }
    var emp2 = {
        name: "B",
        id: "456",
    }
    var x = employee.details.call(emp2, "Manager", "4 years");
    document.getElementById("GFG").innerHTML = x;
</script>

Output:

B 456
Manager
4 years

Example 3: This example describes binding a function to an object.

Javascript




<script>
    var obj = {a: 12, b: 13};
    function sum() {
     return this.a + this.b;
    }
    sum.call(obj);
</script>

Output:
12 13

Supported Browser:

  • Google Chrome 1.0
  • Firefox 1.0
  • Microsoft Edge 12.0
  • Internet Explorer 5.5
  • Opera 4.0
  • Safari 1.0

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!