JavaScript functionStatement
JavaScript Statement Reference
Example
Declare a function that outputs "Hello World" to the element with id="demo" when the function is called:
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
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:
return Math.PI;
}
Output:
Try it Yourself »
Example
Return the value of a multiplied by b:
return a * b;
}
Try it Yourself »
Example
Call the function with different parameters to output different results.
Convert Fahrenheit to Celsius:
return (5/9) * (fahrenheit-32);
}
Try it Yourself »
Example
Functions can be used as variables.
Instead of:
text = "The temperature is " + temp + " Centigrade";
You can also do this:
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:
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":
<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:
Try it Yourself »
Example
After the function expression is stored in a variable, the variable can be used as a function:
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 Statement Reference Other Extensions