Defining functions

There is many ways to define a function. Here's a list of most commoly used ones.

Basic declaring

function robot(diamond) {  
   console.log(diamond)
}

Function expressions

Difference to variable scope handling, function expressions are not hoisted in the start of the scope.

Benefit of named functions is that on error situations stack trace will contain the name of the function. In this example pete is the name of the function.

// Anonymous function expression
var robot = function(diamond) {  
  console.log(diamond)
}

// Named function expression
var robot = function pete(diamond) {  
  console.log(diamond)
}

Immediately Invokable Function Expressions (IIFE)

When function needs to be invoked as soon as it's declared.

(function() {
    console.log(diamond)
})(diamond);

Fatarrow

This is ES6 short form for function declaration. If there is only one parameter, parentheses are not needed. Same goes with statement block. With only one expression, brackets are not needed.

(diamond) => {
console.log(diamond);  
}

diamond => console.log(diamond)