JavaScript | Function Call

It is a predefined javascript method, which is used to write methods for different objects. It calls the method, taking the owner object as argument. The keyword this refers to the “owner” of the function or the object it belongs to.

Syntax:

call()

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



Example 1: It uses call() method to call the employee as argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.