JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow function

function statement

Functions are created using the global object constructor new Function(), via declaration using the function statement, or by function expression using the function operator.

All functions are Function objects undernath the covers, but as mentioned above there are different ways to set up the function creation.

Description

The function statement allows us to create a Function declaration using a statement.

  • Function objects created this way are parsed with the rest of The JavaScript code and so are more efficient than functions created using the constructor creation method.

Syntax


Signature Description
function functionName ([arg1[, arg2[, ... argN]){
   statements;
}
Allows us to create a Function declaration using a statement.

Parameters


Parameter Description
arg1, arg2, ..., argNFormal argument names to be used by the function:
  • A comma(,) delimited list of strings that correspond to valid JavaScript identifiers ('a', 'b', 'c',...)
  • A comma(,) delimited string where each entry corresponds to a valid JavaScript identifier ('a, b, c...')
  • Arguments passed are treated as the names of the parameter identifiers of the function. The function is created with the arguments in the order in which they are passed.
statementsThe JavaScript statements that comprise the function definition.

Examples

The code below gives an example of using the function statement.



*/
 / Create a function statement that takes an argument and
 / returns the square of the argument.
 /*
function show_functionStatement() { 
  function squareNumber(a) {
    return a * a;
  }
  alert(squareNumber(9));
  alert(squareNumber(13));
}

Press the button below to action the above code:



Related Tutorials

JavaScript Intermediate Tutorials - Lesson 8 - Functions



  

go to home page Homepage go to top of page Top