JavaScript function Statement | Rookie Tutorial
JavaScript Statements 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
More examples are included at the bottom of this article.
Definition and Usage
The function statement is used to declare a function.
After declaring a function, we can call it whenever needed.
In JavaScript, functions are objects and also have properties and methods.
Functions can also be defined using expressions (see Function Definitions).
Read our JavaScript tutorial to learn more about functions. Start with JavaScript Functions and JavaScript Scope. For more detailed information, see Function Definitions, Parameters, Invocation, and Closures.
Tips: Use the return statement to return a value from a function.
Browser Support
| Statement | |||||
|---|---|---|---|---|---|
| function | Yes | Yes | Yes | Yes | Yes |
Syntax
function functionName(parameters) {
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 rules as variable names). |
| parameters | Optional. Specifies one or more parameter names, separated by commas (,). When the function is called, it receives actual values. Inside the function, parameters act as local variables. Note: If a parameter is not provided when calling the function, its value will be 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 product of a and b:
function myFunction(a, b) {
return a * b;
}
Example
Call the function with different arguments to produce different results.
Convert Fahrenheit to Celsius:
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
Example
Functions can be used as values.
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 was called:
function myFunction(a, b) {
return arguments.length;
}
Example
Call a function by clicking a button; after execution, "Hello World" will be displayed in 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.
Function expressions can be stored in variables:
var x = function (a, b) {return a * b};
Example
After storing a function expression 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 Definitions
JavaScript Tutorial: JavaScript Function Parameters
JavaScript Tutorial: JavaScript Function Invocation
JavaScript Tutorial: JavaScript Function Closures
JavaScript Reference: JavaScript return Statement
YouTip