JavaScript functionStatement

JavaScript 语句参考手册JavaScript Statement Reference

Example

Declare a function that outputs "Hello World" to the element with id="demo" when the function is called:

function myFunction() { // Declare a function
    document.getElementById("demo").innerHTML = "Hello World!";
}
 
myFunction(); // Call the function

Try it Yourself »

More examples are included at the bottom of this article.


Definition and Usage

The function statement is used to declare a function.

After the function is declared, we can call it whenever needed.

In JavaScript, functions are objects. Functions also have properties and methods.

Functions can also be defined by expressions (seeFunction Definitions)。

Please read our JavaScript tutorial to learn more about functions. First, we can take a look atJavaScript FunctionsandJavaScript Scope. For more details, you can seeFunction Definitions, Parameters, InvocationandClosures。

Tip:Usereturnstatement to return the value of a function.


Browser Support

Statement
function Yes Yes Yes Yes Yes


Syntax

function functionName(parameters) {
    The code to be executed
}

Parameter Values

Parameter Description
functionName Required. Specifies the name of the function. Function names can contain letters, digits, underscores, and dollar signs (same as variable naming rules).
parameters Optional. Specifies one or more parameter names, multiple parameters can be separated by commas (,).

The function receives actual values when called. Inside the function, parameters are local variables.

Note:If no parameters are specified when calling the function, the value of unspecified parameters will be set toundefined

Technical Details

JavaScript Version: 1.0


实例

More Examples

Example

Return the value of PI:

function myFunction() {
    return Math.PI;
}

Output:

3.141592653589793

Try it Yourself »

Example

Return the value of a multiplied by b:

function myFunction(a, b) {
    return a * b;
}

Try it Yourself »

Example

Call the function with different parameters to output different results.

Convert Fahrenheit to Celsius:

function toCelsius(fahrenheit) {
    return (5/9) * (fahrenheit-32);
}

Try it Yourself »

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 Yourself »

Example

JavaScript functions have a built-in object, 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 Yourself »

Example

Click the button to call the function. After the function executes, it will output "Hello World" on the element with id="demo":

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

Try it Yourself »

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 Yourself »

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 Yourself »


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:JavaScript return Statement


JavaScript 语句参考手册JavaScript Statement Reference Other Extensions