Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 845193 of function

  • Revision slug: Web/JavaScript/Reference/Statements/function
  • Revision title: function
  • Revision id: 845193
  • Created:
  • Creator: Sebastianz
  • Is current revision? No
  • Comment Replaced static text for specification by macros and added ES3 spec

Revision Content

{{jsSidebar("Statements")}}

The function declaration defines a function with the specified parameters.

You can also define functions using the {{jsxref("Function")}} constructor and a {{jsxref("Operators/function", "function expression")}}.

Syntax

function name([param,[, param,[..., param]]]) {
   [statements]
}
name
The function name.
param
The name of an argument to be passed to the function. A function can have up to 255 arguments.
statements
The statements which comprise the body of the function.

Description

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

A function can also be created using an expression (see {{jsxref("Operators/function", "function expression")}}).

By default, functions return undefined. To return any other value, the function must have a {{jsxref("Statements/return", "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. Therefore they should not be used, for conditional creation use function expressions.

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() {
  console.log("foo");
}

Note that {{jsxref("Operators/function", "function expressions")}} are not hoisted:

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

var notHoisted = function() {
   console.log("bar");
};

Examples

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;
}

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}} {{Spec2('ES6')}}  
{{SpecName('ES5.1', '#sec-13', 'Function definition')}} {{Spec2('ES5.1')}}  
{{SpecName('ES3', '#sec-13', 'Function definition')}} {{Spec2('ES3')}}  
{{SpecName('ES1', '#sec-13', 'Function definition')}} {{Spec2('ES1')}} Initial definition. Implemented in JavaScript 1.0.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

  • {{jsxref("Functions_and_function_scope", "Functions and function scope")}}
  • {{jsxref("Function")}}
  • {{jsxref("Operators/function", "function expression")}}
  • {{jsxref("Statements/function*", "function* statement")}}
  • {{jsxref("Operators/function*", "function* expression")}}
  • {{jsxref("Functions/Arrow_functions", "Arrow functions")}}
  • {{jsxref("GeneratorFunction")}}

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<p>The <strong>function declaration</strong> defines a function with the specified parameters.</p>

<p>You can also define functions using the {{jsxref("Function")}} constructor and a {{jsxref("Operators/function", "function expression")}}.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
function <em>name</em>([<em>param</em>,[, <em>param</em>,[..., <em>param</em>]]]) {
   [<em>statements</em>]
}
</pre>

<dl>
 <dt><code>name</code></dt>
 <dd>The function name.</dd>
</dl>

<dl>
 <dt><code>param</code></dt>
 <dd>The name of an argument to be passed to the function. A function can have up to 255 arguments.</dd>
</dl>

<dl>
 <dt><code>statements</code></dt>
 <dd>The statements which comprise the body of the function.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>A function created with a function declaration is a <code>Function</code> object and has all the properties, methods and behavior of <code>Function</code> objects. See {{jsxref("Function")}} for detailed information on functions.</p>

<p>A function can also be created using an expression (see {{jsxref("Operators/function", "function expression")}}).</p>

<p>By default, functions return <code>undefined</code>. To return any other value, the function must have a {{jsxref("Statements/return", "return")}} statement that specifies the value to return.</p>

<h3 id="Conditionally_created_functions">Conditionally created functions</h3>

<p>Functions can be conditionally declared, that is, a function statement can be nested within an <code>if</code> 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 <a href="https://kangax.github.io/nfe/#function-statements">this article</a> for an overview. Therefore they should not be used, for conditional creation use function expressions.</p>

<h3 id="Function_declaration_hoisting">Function declaration hoisting</h3>

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

<pre class="brush: js">
hoisted(); // logs "foo"

function hoisted() {
  console.log("foo");
}
</pre>

<p>Note that {{jsxref("Operators/function", "function expressions")}} are not hoisted:</p>

<pre class="brush: js">
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function() {
   console.log("bar");
};
</pre>

<h2 id="Examples">Examples</h2>

<h3 id="Using_function">Using <code>function</code></h3>

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

<pre class="brush: js">
function calc_sales(units_a, units_b, units_c) {
   return units_a*79 + units_b * 129 + units_c * 699;
}
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-13', 'Function definition')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES3', '#sec-13', 'Function definition')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES1', '#sec-13', 'Function definition')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.0.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Functions_and_function_scope", "Functions and function scope")}}</li>
 <li>{{jsxref("Function")}}</li>
 <li>{{jsxref("Operators/function", "function expression")}}</li>
 <li>{{jsxref("Statements/function*", "function* statement")}}</li>
 <li>{{jsxref("Operators/function*", "function* expression")}}</li>
 <li>{{jsxref("Functions/Arrow_functions", "Arrow functions")}}</li>
 <li>{{jsxref("GeneratorFunction")}}</li>
</ul>
Revert to this revision