JavaScript function statements
JavaScript Statements Reference Manual
Examples
Declare a function, the function call output "Hello World" on the id = "demo" element:
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
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 |
---|
More examples
Examples
Returns the value of PI:
return Math.PI;
}
Output:
try it"
Examples
The function is called using different parameters, output different results.
Convert Fahrenheit to Celsius:
return (5/9) * (fahrenheit-32);
}
try it"
Examples
Function can be used as a variable.
Instead of:
text = "The temperature is" + temp + "Centigrade";
You can also do this:
try it"
Examples
JavaScript function has a built-in object arguments.
arguments.length property returns a function call when receiving the number of parameters:
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:
<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:
try it"
Examples
After the function expression stored in the variable, the variable can be used as a function Use:
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