Javascript - Function Declaration

About

A function declaration is one of the several grammar syntax that permits to define a function.

The execution of a function declaration can be done before the function declaration definition.

This is not the case with a function expression.

A function declaration defines a function and binds it to a variable in the current scope.

Syntax

  • The function name is mandatory.
  • The function declaration cannot be written inside a code block that is not a declaration function
function name([param,[, param,[..., param]]]) {
   [statements]
}

Function Execution Context

As every function, a function declaration follows the context principle.

// The global context
function globalFunction() {
  
  // Inside another function
  function innerFunction() {}
}

Example

At the top level of a program, the below declaration would create a global function called add.

/* The execution of a function declaration can be done before the function declaration definition */
/* This is not the case with a function expression */
console.log(add(2,3));

/* The function declaration definition */
function add(a,b) {
     return a+b;
};

console.log(add);

Documentation / Reference





Discover More
Javascript - Class (ES6)

This article is Class in Javascript. Class was introduced in ECMAScript 2015. Before the module pattern was used to create a class-like behavior. Classes provide a simpler way to create objects and...
Javascript Function Method
Javascript - Functions

Functions functionalities in Javascript. A function in Javascript is a variable that’s given a reference to the function being declared. The function itself is a value (as an integer or an array) that...
Javascript - Map function (Functional programming)

The functional programming function map in a Javascript context is only an array method as all functional programming javascript method. map With an Arrow function as argument Letters that we...



Share this page:
Follow us:
Task Runner