Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Function Call

  • Last Updated : 15 Nov, 2021

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 as object methods. A function will be the global object if the function is not considered as 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: This example describes the use of the call() method to call the employee as the argument. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>JavaScript Function Call</title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>JavaScript Function Call</h2>
    <p> It calls the employee details of emp2 </p>
  
    <p id="GFG"></p>
  
      
    <!-- Script to use call() method and display
        the emp2 details -->
    <script>
    var employee = {
        details: function() {
            return this.name + " " + this.id;
        }
    }
    var emp1 = {
        name: "Geeks",
        id: "234412",
    }
    var emp2 = {
        name: "G4G",
        id: "434556",
    }
    var x = employee.details.call(emp2);
    document.getElementById("GFG").innerHTML = x;
    </script>
</body>
  
</html>

Output:

Function Call() Method

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

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title> The call() Method with Arguments </title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>JavaScript Function Call</h2>
    <p> It calls the employee details of emp2 </p>
  
    <p id="GFG"></p>
  
      
    <script>
    var employee = {
        details: function(designation, experience) {
            return this.name 
            + " " 
            + this.id + "<br>" 
            + designation 
            + "<br>" 
            + experience;
        }
    }
    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>
</body>
  
</html>

Output:

Function Call() Method with an argument

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!