The Function Type

Functions are objects. Each function is an instance of the Function type. function type has properties and methods. function names are pointers to function objects.

Functions are typically defined using function-declaration syntax.


function sum (num1, num2) { 
    return num1 + num2; 
}

We can rewrite the code above as:


var sum = function(num1, num2){ 
    return num1 + num2; 
};

The following code uses the Function constructor:

var sum = new Function("num1", "num2", "return num1 + num2");

Multiple names for a single function:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var addSomeNumber = function (num){
            return num + 100; 
        }; 
        
        addSomeNumber = function (num) {
            return num + 200; 
        }; 
        
        var result = addSomeNumber(100); //300 
        document.writeln(result);
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

From the code you can see that the last function wins.

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()