Dichiarazione di funzioni

Questa traduzione è incompleta. Aiutaci a tradurre questo articolo dall’inglese

La dichiarazione di una funzione definisce una funzione con i parametri specificati

You can also define functions using the Function constructor and a function expression.


function name([param,[, param,[..., param]]]) {
The function name.
The name of an argument to be passed to the function. Maximum number of arguments varies in different engines.
The statements which comprise the body of the function.


A function created with a function declaration is a Function object and has all the properties, methods and behavior of Function objects. See Function for detailed information on functions.

A function can also be created using an expression (see function expression).

By default, functions return undefined. To return any other value, the function must have a return statement that specifies the value to return.

Conditionally created functions

Functions can be conditionally declared, that is, a function statement can be nested within an if statement. Most browsers other than Mozilla will treat such conditional declarations as an unconditional declaration and create the function whether the condition is true or not, see this article for an overview. For this reason, they should not be used — for conditional creation use function expressions instead.

Function declaration hoisting

Function declarations in JavaScript are hoisting the function definition. You can use the function before you declared it:

hoisted(); // logs "foo"

function hoisted() {

Note that function expressions are not hoisted:

notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function() {


Using function

The following code declares a function that returns the total amount of sales, when given the number of units sold of products a, b, and c.

function calc_sales(units_a, units_b, units_c) {
   return units_a * 79 + units_b * 129 + units_c * 699;


Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'Function definitions' in that specification.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Function definitions' in that specification.
ECMAScript 5.1 (ECMA-262)
The definition of 'Function definition' in that specification.
ECMAScript 3rd Edition (ECMA-262)
The definition of 'Function definition' in that specification.
ECMAScript 1st Edition (ECMA-262)
The definition of 'Function definition' in that specification.
Standard Initial definition. Implemented in JavaScript 1.0.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
Allowed in sloppy mode 49.0 ?        
Trailing comma in parameters ? ? 52.0 (52.0) ? ? ?
Feature Android Android Webview Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
Allowed in sloppy mode No support




Trailing comma in parameters ? ? ? 52.0 (52.0) ? ? ? ?

See also