Latest web development tutorials

JavaScript function statements

JavaScript Statements Reference Manual JavaScript Statements Reference Manual

Examples

Declare a function, the function call output "Hello World" on the id = "demo" element:

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

myFunction (); // call the function

try it"

Bottom of this article contains more examples.


Definition and Usage

function statement to declare a function.

After the function declaration, we can call on when needed.

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

Function can also be defined by the expression (you can view the function definition ).

Please read our JavaScript tutorials to learn more about the function of the content. First, we can first understand JavaScript function and JavaScript scope . You can view more detailed data about the function definition , parameters , call and closures .

Tip: Use the return statement to return value of the function.


Browser Support

Statements
function Yes Yes Yes Yes Yes


grammar

functionfunctionName (parameter s) {
Code execution
}

Parameter Value

parameter description
functionName have to. Specifies the name of the function. The function name can contain letters, numbers, underscores, and dollar sign (variable naming conventions as well)
parameters Optional. Specifies the name of one or more parameters, a plurality of parameters can be a comma (,) separated.

Function when the real value of the received call. Inside the function, the parameters are local variables.

Note: If not specified when calling the function parameter for the specified parameter value is set to undefined

technical details

JavaScript version: 1.0


Examples

More examples

Examples

Returns the value of PI:

function myFunction () {
return Math.PI;
}

Output:

3.141592653589793

try it"

Examples

Returns a ride on the value of b:

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

try it"

Examples

The function is called using different parameters, output different results.

Convert Fahrenheit to Celsius:

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

try it"

Examples

Function can be used as a variable.

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"

Examples

JavaScript function has a built-in object arguments.

arguments.length property returns a function call when receiving the number of parameters:

function myFunction (a, b) {
return arguments.length;
}

try it"

Examples

Click the button to call the function after the function will output "Hello World" on the id = "demo" element:

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

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

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

try it"

Examples

JavaScript functions can be defined as an expression.

Function expression can be stored in variables:

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

try it"

Examples

After the function expression stored in the variable, the variable can be used as a function Use:

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

try it"


Related Pages

JavaScript Tutorials: JavaScript function

JavaScript Tutorials: JavaScript Scope

JavaScript Tutorials: JavaScript function definition

JavaScript Tutorials: JavaScript function parameters

JavaScript Tutorials: JavaScript function call

JavaScript Tutorials: JavaScript function closures

JavaScript Reference Manual: JavaScript statement return


JavaScript Statements Reference Manual JavaScript Statements Reference Manual