JavaScript function Statement
JavaScript statement reference manual
Example
Declare a function that outputs "Hello World" to the element with id="demo" when called:
function myFunction() { // Declare a function
document.getElementById("demo").innerHTML = "Hello World!";
}
myFunction(); // Call the function
Try it »
The bottom of this article contains more examples.
Definition and Usage
The function statement is used to declare a function.
After the function is declared, we can call it when needed.
In JavaScript, functions are objects, and they have properties and methods.
Functions can also be defined by expressions (see Function definition).
Please read our JavaScript tutorial to learn more about functions. First, we can get to know JavaScript Functions and JavaScript Scope. For more detailed content, see Function definition, Parameters, called and Closures.
Tip: Use return The statement to return the function's value.
Browser support
| Statement | |||||
|---|---|---|---|---|---|
| function | Yes | Yes | Yes | Yes | Yes |
Syntax
function
functionName
(
parameters
)
{
The code to be executed
}
Parameter value
| Parameters | Description |
|---|---|
| functionName | Required. Specifies the name of the function. The function name may contain letters, digits, underscores, and the dollar sign (the same naming rules apply to variables) |
| parameters | Optional. Specifies one or more parameter names; multiple parameters are separated by commas (,). |
函数在调用时会接收真正的值。在函数内,参数是局部变量。
**Note:** If parameters are not specified when calling a function, the value of the specified parameter will be set to **undefined** |
Technical Details
| JavaScript version: | 1.0 |
|---|
More examples
Example
Returns the value of PI:
function myFunction() {
return Math.PI;
}
Output result:
3.141592653589793
Try it »
Example
Returns the value of a times b:
function myFunction(a, b) {
return a * b;
}
Try it »
Example
Call the function with different arguments to produce different results.
Convert Fahrenheit temperature to Celsius temperature:
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
Try it »
Example
Functions can be used as variables.
Instead of:
temp = toCelsius(32);
text = "The temperature is " + temp + " Centigrade";
You can also do this:
text = "The temperature is " + toCelsius(32) + " Centigrade";
Try it »
Example
JavaScript functions have a built-in object called arguments.
The arguments.length property returns the number of arguments received when the function is called:
function myFunction(a, b) {
return arguments.length;
}
Try it »
Example
Click the button to call the function, which outputs "Hello World" to the element with id="demo" after execution:
Try it »
Example
JavaScript functions can be defined as an expression.
Function expressions can be stored in variables:
var x = function (a, b) {return a * b};
Try it »
Example
After the function expression is stored in a variable, the variable can be used as a function:
var x = function (a, b) {return a * b};
var z = x(4, 3);
Try it »
Related pages
JavaScript tutorial: JavaScript Functions
JavaScript tutorial: JavaScript Scope
JavaScript tutorial: JavaScript Function Definitions
JavaScript tutorial: JavaScript Function Parameters
JavaScript tutorial: JavaScript Function Invocation
JavaScript tutorial: JavaScript Function Closures
JavaScript Reference Manual: JavaScript Return Statements
JavaScript statement reference manual