Javascript functions

Function declaration

Javascript provides a few ways to define functions.

The way a function is defined influences when the function is available to be invoked and how it behaves as well as on which objects the function can be invoked.

Function declarations

Function declarations must be placed on their own as a separate statement.
It start with the function keyword, followed by the function name and a listof optional arguments, enclosed in parenthesis.

Following the argument list, are brackets that enclose the function body.

function myFunction(a, b) {
  return a + b;
}

Function declarations can be nested inside another function, like this example:

function enclosingFunction() {   
                                 
  function inside() {            
    return "Hello world";        
  }                              
                                 
  return inside();               
                                 
}                                
                                 
console.log(enclosingFunction()); // Hello world

Another common way to declare functions is using a function expression.

We are going to look at function expressions in detail in the next post.