JavaScript function statement

Definition and Usage

The function statement declares a function.

Declared functions are 'saved for later use' and will be executed when called later.

In JavaScript, functions are objects that have both properties and methods.

Functions can also be defined using expressions (see function definition).

Please read our JavaScript tutorial to learn all about functions you need to know. Start with the chapters on JavaScript functions and JavaScript scope. For more detailed information, please read our tutorials on function definition, parameters, invocation, and closures.

Tip:Please use the return statement to return a value from the function.

Example

Declare a function and output "Hello World" in the element with id="demo" when calling the function:

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

Try It Yourself

More TIY examples are available at the bottom of the page.

Syntax

function functionName(parameters) {
  code to be executed
}

Parameter value

Parameter Description
functionName Required. Specifies the name of the function, which can be 'saved for later use'. The function name can contain letters, numbers, underscores, and dollar signs (rules are the same as variables).
parameters

Optional. Specify a group of zero or more parameter names, separated by commas.

Function parameters are the names listed in the function definition.

Function parameters are the actual values received by the function when it is called. Inside the function, parameters are used as local variables.

Note:If a parameter is missing when calling a function, the value of the missing parameter will be set to undefined.

Technical Details

JavaScript Version: ECMAScript 1

More Examples

Example

Return the value of PI:

function myFunction() {
  return Math.PI;
}

Try It Yourself

Example

Return the product of a and b:

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

Try It Yourself

Example

By using functions, you can reuse the same code with different parameters to produce different results multiple times.

Convert Fahrenheit to Centigrade:

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 use:
text = "The temperature is " + toCelsius(32) + " Centigrade";

Try It Yourself

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 Yourself

Example

Click the button to call the function, which will output "Hello World" 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>

Try It Yourself

Example

JavaScript functions can also be defined using expressions.

Function expressions can be stored in variables:

var x = function (a, b) {return a * b};

Try It Yourself

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);

Try It Yourself

Browser Support

Statements Chrome IE Firefox Safari Opera
function Support Support Support Support Support

Related Pages

JavaScript Tutorial:JavaScript Function

JavaScript Tutorial:JavaScript Scope

JavaScript Tutorial:JavaScript Function Definition

JavaScript Tutorial:JavaScript Function Parameters

JavaScript Tutorial:JavaScript Function Call

JavaScript Tutorial:JavaScript Function Closures

JavaScript Reference Manual:JavaScript return Statement