# JavaScript Overview – Functions

A function is a special kind of object, which has executable code associated with it, and can be invoked to run that code and return a value. Functions, like arrays, behave differently than objects, and have a special language syntax for working with them. Most importantly, they are true values, and programs can treat them like regular objects.

Functions are defined using the function keyword, and arguments and return are optional. The following forms are equivalent:

                function  sum(x, y) {
      return x + y;
    }
    var sum = function (x, y) {
      return x + y;
    }

Within JavaScript, functions are variadic, that is any function can take a variable number of arguments; the arguments keyword is an array of the current function’s arguments. The following demonstrates this:

                function sum() {
      var i, total = 0;
      for (i=0; i < arguments.length; i++) {
        total += (+arguments[i]);
      }
      return total;
    }
    var z = sum(1,2, "3.14");	// 6.14