Skip to content

Latest commit

 

History

History
98 lines (80 loc) · 2.63 KB

File metadata and controls

98 lines (80 loc) · 2.63 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
function Statement (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
function_JavaScriptKeyword
JavaScript
TypeScript
DHTML
new operator
declaring functions, syntax
function statement
declaring functions
cc9cfd43-1305-41c8-ad67-545d20f4fafe
20
mikejo5000
mikejo
ghogen

function Statement (JavaScript)

Declares a new function.

Syntax

function functionname ([arg1 [, arg2 [,...[, argN]]]]) {  
    statements  
}   

Parameters

functionname
Required. The name of the function.

arg1...argN
Optional. An optional, comma-separated list of arguments the function understands.

statements
Optional. One or more[!INCLUDEjavascript] statements.

Remarks

Use the function statement to declare a function for later use. The code that is contained in statements is not executed until the function is called from elsewhere in the script.

The return statement is used to return a value from the function. You do not have to use a return statement; the program will return when it reaches the end of the function. If no return statement is executed in the function, or if the return statement has no expression, the function returns the value undefined.

Note

When you call a function, be sure to include the parentheses and any required arguments. Calling a function without parentheses returns a reference to the function, not the results of the function.

Example

The following example illustrates the use of the function statement.

function myfunction (arg1, arg2) {  
    var r = arg1 * arg2;  
    return(r);  
}  

Example

A function can be assigned to a variable. This is illustrated in the following example.

function AddFive(x) {  
    return x + 5;  
}  
  
function AddTen(x) {  
    return x + 10;  
}  
  
var condition = false;  
  
var MyFunc;  
if (condition) {  
    MyFunc = AddFive;  
}  
else {  
    MyFunc = AddTen;  
}  
  
var result = MyFunc(123);  
// Output: 133  

Requirements

[!INCLUDEjsv1]

See Also

new Operator