JavaScript function
Statement
JavaScript Statement Reference Manual
Example
Declare a function that outputs "Hello World" on the element with id="demo" when called:
function myFunction() { // Declare a function
document.getElementById("demo").innerHTML = "Hello World!";
}
myFunction(); // Call the function
More examples are included at the bottom of this article.
Definition and Usage
The function
statement is used to declare a function.
After a function is declared, it can be called whenever needed.
In JavaScript, functions are objects, and they have properties and methods.
Functions can also be defined by expressions (see Function Definition).
For more information about functions, please read our JavaScript tutorial. Start by understanding JavaScript Functions and JavaScript Scope. For detailed content, see Function Definition, Parameters, Invocation, and Closures.
Tip: Use the return statement to return a value from the function.
Browser Support
Statement | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
function | Yes | Yes | Yes | Yes | Yes |
Syntax
Parameter Values
Parameter | Description |
---|---|
functionName | Required. Specifies the function's name. The function name can include letters, digits, underscores, and dollar signs (same rules as variable names). |
parameters | Optional. Specifies one or more parameter names, separated by commas. The function receives actual values when called. Inside the function, parameters act as local variables. Note: If a parameter is not specified when the function is called, its value is set to undefined. |
Technical Details
| JavaScript Version: | 1.0 | | --- | --- |
More Examples
Example
Return the value of PI:
function myFunction() {
return Math.PI;
}
Output:
3.141592653589793
Example
Return the value of a multiplied by b:
function myFunction(a, b) {
return a * b;
}
Example
Call the function with different arguments to get different results.
Convert Fahrenheit to Celsius:
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
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";
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;
}
Example
Click a button to call a function that outputs "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>
Example
JavaScript functions can be defined as expressions.
A function expression can be stored in a variable:
var x = function (a, b) {return a * b};
Example
After a 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);
Related Pages
JavaScript Tutorial: JavaScript Functions
JavaScript Tutorial: JavaScript Scope
JavaScript Tutorial: JavaScript Function Definition
JavaScript Tutorial: JavaScript Function Parameters
JavaScript Tutorial: JavaScript Function Invocation
JavaScript Tutorial: JavaScript Function Closures
JavaScript Reference Manual: JavaScript return Statement