Declares a new function.
Syntaxfunction functionName ([ arg1 [, arg2 [,...[, argN ]]]] ) {
statements
}
The following example illustrates the use of the function statement.
function myFunc () {
var r = 3 * 4;
return(r);
}
myFunc();
It’s possible to pass along arguments within the parantheses when calling the function.
function myFunc (name) {
console.log('Hello, ' + name + '!');
}
myFunc('Susan');
A function can be assigned to a variable. This is illustrated in the following example.
function addFive(x) {
return x + 5;
}
function addTen(x) {
return x + 10;
}
var condition = false;
var myFunc;
if (condition) {
myFunc = addFive;
}
else {
myFunc = addTen;
}
var result = myFunc(123);
Remarks
Use the function statement to declare a function for later use. The code that is contained in statements is not executed until the function is called from elsewhere in the script.
The return statement is used to return a value from the function. You do not have to use a return statement; the program will return when it reaches the end of the function. If no return statement is executed in the function, or if the return statement has no expression, the function returns the value undefined.
Note – When you call a function, be sure to include the parentheses and any required arguments. Calling a function without parentheses returns a reference to the function, not the results of the function.
See also Other articles AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4