HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Declare a function that outputs "Hello World" when it is called
The function
statement declares a function.
A declared function is "saved for later use", and will be executed later, when it is invoked (called).
In JavaScript, functions are objects, and they have both properties and methods.
A function can also be defined using an expression (See Function Definitions).
Read our JavaScript Tutorial to learn all you need to know about functions. Start with the introduction chapter about JavaScript Functions and JavaScript Scope. For more detailed information, see our Function Section on Function Definitions, Parameters, Invocation and Closures.
Parameter | Description |
functionName | Required. The name of the function. Naming rules: same as JavaScript variables. |
parameters | Optional. A set of arguments (parameter names), separated by commas. The arguments are real values received by the function from the outside. Inside the function, the arguments are used as local variables. If a function is called with a missing argument, the value of the missing argument is set to undefined . |
Return the value of PI:
function myFunction() {
return Math.PI;
}
Return the product of a and b:
function myFunction(a, b) {
return a * b;
}
A function with different arguments can produce different results.
Convert Fahrenheit to Celsius:
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
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";
JavaScript functions have a built-in object called arguments.
The arguments.length property returns the number of arguments received by the function:
function myFunction(a, b) {
return arguments.length;
}
Click to call a function that outputs "Hello World":
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
When a function expression is stored in a variable, the variable contains a function:
const x = function (a, b) {return a * b};
When a function is stored in a variable, the variable can be used as a function:
const x = function (a, b) {return a * b};
let z = x(4, 3);
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
function
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |