What is a function with the same name as another function in its class called?

Generally speaking, a function is a "subprogram" that can be called by code external (or internal in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are

const myFunction = function () {
  statements
}
0 objects.

For more examples and explanations, see the JavaScript guide about functions.

Description

Every function in JavaScript is a

const myFunction = function () {
  statements
}
0 object. See
const myFunction = function () {
  statements
}
0 for information on properties and methods of
const myFunction = function () {
  statements
}
0 objects.

To return a value other than the default, a function must have a

const myFunction = function () {
  statements
}
4 statement that specifies the value to return. A function without a return statement will return a default value. In the case of a constructor called with the
const myFunction = function () {
  statements
}
5 keyword, the default value is the value of its
const myFunction = function () {
  statements
}
6 parameter. For all other functions, the default return value is
const myFunction = function () {
  statements
}
7.

The parameters of a function call are the function's arguments. Arguments may be passed by value (in the case of primitive values) or by reference (in the case of objects). This means that if a function reassigns a primitive type parameter, the value won't change outside the function. In the case of an object type parameter, if its properties are mutated, the change will impact outside of the function. See the following example:

/* Declare the function 'myFunc' */
function myFunc(theObject) {
  theObject.brand = "Toyota";
}

/*
 * Declare variable 'mycar';
 * create and initialize a new Object;
 * assign reference to it to 'mycar'
 */
const mycar = {
  brand: "Honda",
  model: "Accord",
  year: 1998
};

/* Logs 'Honda' */
console.log(mycar.brand);

/* Pass object reference to the function */
myFunc(mycar);

/*
 * Logs 'Toyota' as the value of the 'brand' property
 * of the object, as changed to by the function.
 */
console.log(mycar.brand);

The

const myFunction = function () {
  statements
}
6 keyword does not refer to the currently executing function, so you must refer to
const myFunction = function () {
  statements
}
0 objects by name, even within the function body.

Defining functions

There are several ways to define functions:

The function declaration

There is a special syntax for declaring functions (see function statement for details):

function name([param[, param[, ... param]]]) {
  statements
}

const myFunction = function namedFunction() {
  statements
}
0

The function name.

const myFunction = function namedFunction() {
  statements
}
1

The name of an argument to be passed to the function.

const myFunction = function namedFunction() {
  statements
}
2

The statements comprising the body of the function.

The function expression

A function expression is similar to and has the same syntax as a function declaration (see function expression for details). A function expression may be a part of a larger expression. One can define "named" function expressions (where the name of the expression might be used in the call stack for example) or "anonymous" function expressions. Function expressions are not hoisted onto the beginning of the scope, therefore they cannot be used before they appear in the code.

function [name]([param[, param[, ... param]]]) {
  statements
}

const myFunction = function namedFunction() {
  statements
}
0

The function name. Can be omitted, in which case the function becomes known as an anonymous function.

const myFunction = function namedFunction() {
  statements
}
1

The name of an argument to be passed to the function.

const myFunction = function namedFunction() {
  statements
}
2

The statements comprising the body of the function.

Here is an example of an anonymous function expression (the

const myFunction = function namedFunction() {
  statements
}
0 is not used):

const myFunction = function () {
  statements
}

It is also possible to provide a name inside the definition in order to create a named function expression:

const myFunction = function namedFunction() {
  statements
}

One of the benefits of creating a named function expression is that in case we encountered an error, the stack trace will contain the name of the function, making it easier to find the origin of the error.

As we can see, both examples do not start with the

const myFunction = function namedFunction() {
  statements
}
7 keyword. Statements involving functions which do not start with
const myFunction = function namedFunction() {
  statements
}
7 are function expressions.

When functions are used only once, a common pattern is an IIFE (Immediately Invoked Function Expression).

(function () {
  statements
})();

IIFE are function expressions that are invoked as soon as the function is declared.

The generator function (function*) declaration

There is a special syntax for generator function declarations (see

const myFunction = function namedFunction() {
  statements
}
9 for details):

function* name([param[, param[, ... param]]]) {
  statements
}

const myFunction = function namedFunction() {
  statements
}
0

The function name.

const myFunction = function namedFunction() {
  statements
}
1

The name of an argument to be passed to the function.

const myFunction = function namedFunction() {
  statements
}
2

The statements comprising the body of the function.

The generator function (function*) expression

A generator function expression is similar to and has the same syntax as a generator function declaration (see

(function () {
  statements
})();
3 for details):

function* [name]([param[, param[, ... param]]]) {
  statements
}

const myFunction = function namedFunction() {
  statements
}
0

The function name. Can be omitted, in which case the function becomes known as an anonymous function.

const myFunction = function namedFunction() {
  statements
}
1

The name of an argument to be passed to the function.

const myFunction = function namedFunction() {
  statements
}
2

The statements comprising the body of the function.

The arrow function expression (=>)

An arrow function expression has a shorter syntax and lexically binds its

const myFunction = function () {
  statements
}
6 value (see arrow functions for details):

([param[, param]]) => {
  statements
}

param => expression

(param) => expression

const myFunction = function namedFunction() {
  statements
}
1

The name of an argument. Zero arguments need to be indicated with

(function () {
  statements
})();
9. For exactly one argument, the parentheses are not required. (like
function* name([param[, param[, ... param]]]) {
  statements
}
0)

const myFunction = function namedFunction() {
  statements
}
2 or
function* name([param[, param[, ... param]]]) {
  statements
}
2

Multiple statements need to be enclosed in brackets. A single expression requires no brackets. The expression is also the implicit return value of the function.

The Function constructor

Note: Using the

const myFunction = function () {
  statements
}
0 constructor to create functions is not recommended since it needs the function body as a string which may prevent some JS engine optimizations and can also cause other problems.

As all other objects,

const myFunction = function () {
  statements
}
0 objects can be created using the
const myFunction = function () {
  statements
}
5 operator:

new Function (arg1, arg2, ... argN, functionBody)

function* name([param[, param[, ... param]]]) {
  statements
}
6

Zero or more names to be used by the function as formal parameters. Each must be a proper JavaScript identifier.

function* name([param[, param[, ... param]]]) {
  statements
}
7

A string containing the JavaScript statements comprising the function body.

Invoking the

const myFunction = function () {
  statements
}
0 constructor as a function (without using the
const myFunction = function () {
  statements
}
5 operator) has the same effect as invoking it as a constructor.

The GeneratorFunction constructor

Note:

function* [name]([param[, param[, ... param]]]) {
  statements
}
0 is not a global object, but could be obtained from generator function instance (see
function* [name]([param[, param[, ... param]]]) {
  statements
}
0 for more detail).

Note: Using the

function* [name]([param[, param[, ... param]]]) {
  statements
}
0 constructor to create functions is not recommended since it needs the function body as a string which may prevent some JS engine optimizations and can also cause other problems.

As all other objects,

function* [name]([param[, param[, ... param]]]) {
  statements
}
0 objects can be created using the
const myFunction = function () {
  statements
}
5 operator:

function name([param[, param[, ... param]]]) {
  statements
}
0

function* name([param[, param[, ... param]]]) {
  statements
}
6

Zero or more names to be used by the function as formal argument names. Each must be a string that conforms to the rules for a valid JavaScript identifier or a list of such strings separated with a comma; for example

function* [name]([param[, param[, ... param]]]) {
  statements
}
6,
function* [name]([param[, param[, ... param]]]) {
  statements
}
7, or
function* [name]([param[, param[, ... param]]]) {
  statements
}
8.

function* name([param[, param[, ... param]]]) {
  statements
}
7

A string containing the JavaScript statements comprising the function definition.

Invoking the

function* [name]([param[, param[, ... param]]]) {
  statements
}
0 constructor as a function (without using the
const myFunction = function () {
  statements
}
5 operator) has the same effect as invoking it as a constructor.

Function parameters

Default parameters

Default function parameters allow formal parameters to be initialized with default values if no value or

const myFunction = function () {
  statements
}
7 is passed. For more details, see default parameters.

Rest parameters

The rest parameter syntax allows representing an indefinite number of arguments as an array. For more details, see rest parameters.

The arguments object

You can refer to a function's arguments within the function by using the

([param[, param]]) => {
  statements
}

param => expression

(param) => expression
3 object. See arguments.

([param[, param]]) => {
  statements
}

param => expression

(param) => expression
3

An array-like object containing the arguments passed to the currently executing function.

([param[, param]]) => {
  statements
}

param => expression

(param) => expression
5

The currently executing function.

([param[, param]]) => {
  statements
}

param => expression

(param) => expression
6

The number of arguments passed to the function.

Defining method functions

Getter and setter functions

You can define getters (accessor methods) and setters (mutator methods) on any standard built-in object or user-defined object that supports the addition of new properties. The syntax for defining getters and setters uses the object literal syntax.

get

Binds an object property to a function that will be called when that property is looked up.

set

Binds an object property to a function to be called when there is an attempt to set that property.

Method definition syntax

In object literals, you are able to define own methods in a shorter syntax, similar to the getters and setters. See method definitions for more information.

function name([param[, param[, ... param]]]) {
  statements
}
1

Constructor vs. declaration vs. expression

Compare the following:

A function defined with the

const myFunction = function () {
  statements
}
0 constructor assigned to the variable
([param[, param]]) => {
  statements
}

param => expression

(param) => expression
8:

function name([param[, param[, ... param]]]) {
  statements
}
2

A function declaration of a function named

([param[, param]]) => {
  statements
}

param => expression

(param) => expression
8:

function name([param[, param[, ... param]]]) {
  statements
}
3

A function expression of an anonymous function assigned to the variable

([param[, param]]) => {
  statements
}

param => expression

(param) => expression
8:

function name([param[, param[, ... param]]]) {
  statements
}
4

A function expression of a function named

new Function (arg1, arg2, ... argN, functionBody)
1 assigned to the variable
([param[, param]]) => {
  statements
}

param => expression

(param) => expression
8:

function name([param[, param[, ... param]]]) {
  statements
}
5

Differences

All do approximately the same thing, with a few subtle differences:

There is a distinction between the function name and the variable the function is assigned to. The function name cannot be changed, while the variable the function is assigned to can be reassigned. The function name can be used only within the function's body. Attempting to use it outside the function's body results in an error (or get another value, if the same name is declared elsewhere). For example:

function name([param[, param[, ... param]]]) {
  statements
}
6

The function name also appears when the function is serialized via its

new Function (arg1, arg2, ... argN, functionBody)
3 method.

On the other hand, the variable the function is assigned to is limited only by its scope, which is guaranteed to include the scope in which the function is declared.

As the

new Function (arg1, arg2, ... argN, functionBody)
4 example shows, the function name can be different from the variable the function is assigned to. They have no relation to each other. A function declaration also creates a variable with the same name as the function name. Thus, unlike those defined by function expressions, functions defined by function declarations can be accessed by their name in the scope they were defined in, as well as in their own body.

A function defined by

new Function (arg1, arg2, ... argN, functionBody)
5 will dynamically have its source assembled, which is observable when you serialize it. For example,
new Function (arg1, arg2, ... argN, functionBody)
6 gives:

function name([param[, param[, ... param]]]) {
  statements
}
7

This is the actual source used to compile the function. However, although the

new Function (arg1, arg2, ... argN, functionBody)
7 constructor will create the function with name
new Function (arg1, arg2, ... argN, functionBody)
8, this name is not added to the scope of the body. The body only ever has access to global variables. For example, the following would result in an error:

function name([param[, param[, ... param]]]) {
  statements
}
8

Unlike functions defined by function expressions or by the

const myFunction = function () {
  statements
}
0 constructor, a function defined by a function declaration can be used before the function declaration itself. For example:

function name([param[, param[, ... param]]]) {
  statements
}
9

A function defined by a function expression or by a function declaration inherits the current scope. That is, the function forms a closure. On the other hand, a function defined by a

const myFunction = function () {
  statements
}
0 constructor does not inherit any scope other than the global scope (which all functions inherit).

function [name]([param[, param[, ... param]]]) {
  statements
}
0

Functions defined by function expressions and function declarations are parsed only once, while those defined by the

const myFunction = function () {
  statements
}
0 constructor are not. That is, the function body string passed to the
const myFunction = function () {
  statements
}
0 constructor must be parsed each and every time the constructor is called. Although a function expression creates a closure every time, the function body is not reparsed, so function expressions are still faster than
function name([param[, param[, ... param]]]) {
  statements
}
03. Therefore the
const myFunction = function () {
  statements
}
0 constructor should generally be avoided whenever possible.

It should be noted, however, that function expressions and function declarations nested within the function generated by parsing a

new Function (arg1, arg2, ... argN, functionBody)
7 constructor's string aren't parsed repeatedly. For example:

function [name]([param[, param[, ... param]]]) {
  statements
}
1

A function declaration is very easily (and often unintentionally) turned into a function expression. A function declaration ceases to be one when it either:

  • becomes part of an expression
  • is no longer a "source element" of a function or the script itself. A "source element" is a non-nested statement in the script or a function body:

function [name]([param[, param[, ... param]]]) {
  statements
}
2

Examples

function [name]([param[, param[, ... param]]]) {
  statements
}
3

Block-level functions

In strict mode, starting with ES2015, functions inside blocks are now scoped to that block. Prior to ES2015, block-level functions were forbidden in strict mode.

function [name]([param[, param[, ... param]]]) {
  statements
}
4

Block-level functions in non-strict code

In a word: Don't.

In non-strict code, function declarations inside blocks behave strangely. For example:

function [name]([param[, param[, ... param]]]) {
  statements
}
5

ES2015 says that if

function name([param[, param[, ... param]]]) {
  statements
}
06 is false, then
function name([param[, param[, ... param]]]) {
  statements
}
07 should never be defined, since the block never executes. However, it's a new part of the standard. Historically, this was left unspecified, and some browsers would define
function name([param[, param[, ... param]]]) {
  statements
}
07 whether the block executed or not.

In strict mode, all browsers that support ES2015 handle this the same way:

function name([param[, param[, ... param]]]) {
  statements
}
07 is defined only if
function name([param[, param[, ... param]]]) {
  statements
}
06 is true, and only exists within that scope of the
function name([param[, param[, ... param]]]) {
  statements
}
11-block.

A safer way to define functions conditionally is to assign a function expression to a variable:

function [name]([param[, param[, ... param]]]) {
  statements
}
6

Examples

Returning a formatted number

The following function returns a string containing the formatted representation of a number padded with leading zeros.

function [name]([param[, param[, ... param]]]) {
  statements
}
7

The following statements call the padZeros function.

function [name]([param[, param[, ... param]]]) {
  statements
}
8

Determining whether a function exists

You can determine whether a function exists by using the

function name([param[, param[, ... param]]]) {
  statements
}
12 operator. In the following example, a test is performed to determine if the
function name([param[, param[, ... param]]]) {
  statements
}
13 object has a property called
function name([param[, param[, ... param]]]) {
  statements
}
14 that is a function. If so, it is used; otherwise, some other action is taken.

function [name]([param[, param[, ... param]]]) {
  statements
}
9

Note that in the

function name([param[, param[, ... param]]]) {
  statements
}
11 test, a reference to
function name([param[, param[, ... param]]]) {
  statements
}
14 is used—there are no brackets "()" after the function name so the actual function is not called.

What is a function with the same name as that of its class called?

A function with same name as class name is known as Constructor. It gets executed when the object is created. You can have multiple constructors (constructor overloading), but in that case, you will have constructors with different set of parameters.

When can two functions have the same name?

Yes, it's called function overloading. Multiple functions are able to have the same name if you like, however MUST have different parameters.