Function apply()

The apply() method accepts two arguments:

  • the value of this
  • an array of arguments.
 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        function sum(num1, num2){ 
            return num1 + num2; 
        } 
        
        function callSum1(num1, num2){ 
            return sum.apply(this, arguments); //passing in arguments object 
        } 
        
        function callSum2(num1, num2){ 
            return sum.apply(this, [num1, num2]); //passing in array 
        } 
        document.writeln(callSum1(10,10)); //20
        document.writeln(callSum2(10,10)); //20 
        
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Essential Types  

Function:
  1. The Function Type
  2. Function Declarations versus Function Expressions
  3. Functions as Values
  4. Returning a function from a function
  5. Function arguments
  6. this for function context
  7. Function caller
  8. Function length property
  9. Function apply()
  10. Function.call() method
  11. Function's bind() method
  12. Function toLocaleString() and toString()